0

我不知道如何隐藏前置跨度标签。我有以下 JQuery 代码,它在表单中将 span 标记作为错误消息添加:

$('.mcEmail').each(function() {
        var mcEmailCheck = $(this).val();
        var mcEmailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        if(!mcEmailCheck.match(mcEmailRegex)) {
            mcResponse('- Incorrect Email format!', true);
            $(this).parent().prepend('<span class="mcCustResponse">- Incorrect Email format!</span>');
            $(this).addClass('mcError').fadeOut().fadeIn();
        }
    });

然后我尝试以下方法:

$('.mcCustResponse').click(function(){
    $(this).fadeOut(1000);
});

这同样适用于所有其他验证。错误消息显示正常。我几乎可以在这些跨度标签上尝试任何东西,但没有任何效果。无法隐藏、淡出、移除等。我做错了什么?

谢谢!

4

3 回答 3

1

你用的是哪个版本的jquery?您需要使用.live().on()取决于版本。问题是当页面加载时您的跨度与类 mcCustResponse 不存在。您拥有的代码块.click()只会在加载时为页面上的元素注册事件。您可以创建一个函数,现在和将来使用.live()or将事件注册到类 mcCustResponse 的所有元素.on()

http://api.jquery.com/on/ - .on()jquery 1.7

http://api.jquery.com/live/ - .live()1.7 之前

于 2011-12-26T22:31:36.490 回答
1
$('.mcCustResponse').live("click", function(){
    $(this).fadeOut(1000);
});

或者

$('.mcCustResponse').bind("click", function(){
    $(this).fadeOut(1000);
});
于 2011-12-26T22:31:53.030 回答
0

先创建跨度,然后添加和操作它怎么样:

var span = '<span class="mcCustResponse">- Incorrect Email format!</span>';
span.hide();
$(this).parent().prepend(span);

...

$(this).parent().find('span').show();

希望这可以帮助!:)

于 2011-12-26T22:31:01.810 回答