簡體   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