繁体   English   中英

使用jquery.validate追加后的jQuery隐藏元素

[英]jQuery hide element after append using jquery.validate

将元素附加到div后,无法隐藏元素。 我已经尝试了一切,并四处寻找答案。 我几乎整天都在上面。

所以我用jquery.validate.js制作了一个自定义的错误代码

email: '<img id="img1" class="RCB" src="images/closeButton.png" /><h3>Incorrect Email</h3><p>Please enter a valid email address</p>'

然后将其附加到我最右边的div #rightBar

errorClass: "rightNotification",
errorElement: "div",

errorPlacement: function(error, element) {
  error.appendTo("#rightBar");
},

然后在单独的js文件中,当单击顶部的关闭按钮时,我尝试对其进行fade()

$(document).ready(function() {
  $(".RCB").live("click", function() {
    $('.rightNotification').hide();
  });
});

我也尝试过

$(document).ready(function() {
  $(".RCB").click(function() {
    $('.rightNotification').hide();
  });
});

和许多其他解决方案。 我基本上希望右关闭按钮(RCB)关闭整个通知。 我真的没有想到我没有尝试过的任何事物,但我希望您能想到些什么。

干杯,马特

任何帮助将不胜感激!

您需要使用事件委托,因为它是动态添加的-在1.7版中不推荐使用live() ,而在1.9版中已将其删除:

$(document).on("click", ".RCB", function() {
   $('.rightNotification').hide();
});

如果可能,将document替换为更靠近容器的元素(以提高效率)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM