1

下面的代码正在删除它找到的第一个 div,而不是预期的 div。

jQuery

// Auto Remove PROMPT Notice Messages 
(function($){
  $(document).ready(function() {
    $("[AutoHide]").each(function() {
      if (!isNaN($(this).attr("AutoHide"))) {
        eval("setTimeout(function() {jQuery('#" + this.id + "').hide();}, " + parseInt($(this).attr('AutoHide')) * 800 + ");");
      }
    });
  });
})(jQuery);  

HTML(或者至少,有问题的区域看起来像这样)

<div id="notify" class="infomsg">
  <p><b>TIP:</b> Some message that should be prompted.
  <input class="close msgbutton" type="button" onclick="deleteline(this)" value="[X]" />
</div>
<div id="notify" AutoHide="5" class="successmsg">
  <p><b>SUCCESS: </b> User form processing was successful!</p>
  <input class="close msgbutton" type="button" onclick="deleteline(this)" value="[X]" />
</div>

现在我不明白为什么 jQuery 函数没有删除带有 AutoHide 属性的 div,而是删除了没有的那个(它的 id 为“notify”)。

我认为罪魁祸首在于这部分代码:

jQuery('#" + this.id + "').hide(); 
4

2 回答 2

1

改用这个

(function($){
    $(document).ready(function() {
        $("[AutoHide]").each(function() {
            var that = this;
            if (!isNaN($(that).attr("AutoHide"))) {
                setTimeout(function() {jQuery(that).hide();}, parseInt($(that).attr('AutoHide')) * 800 );
            }
        });
    });
})(jQuery); 

我建议不要使用相同的 id

于 2013-06-17T21:34:47.310 回答
0

您使用相同的 id 两次。按照规则,HTML 元素的 id 应该是唯一的。

当你使用这个时:jQuery('#" + this.id + "').hide();“this.id”指的是“通知”。但它隐藏了第一个 div,因为那是第一个用 div 渲染的 div id="notify",所以它是第一个找到的。

尝试使用唯一 ID:

<div id="info_notify" class="infomsg"><p><b>TIP:</b> Some message that should be propmted.<input class="close msgbutton" type="button" onclick="deleteline(this)" value="[X]" /></div>

<div id="success_notify" AutoHide="5" class="successmsg"><p><b>SUCCESS: </b> User form processing was successfull!</p> <input class="close msgbutton" type="button" onclick="deleteline(this)" value="[X]" /></div>
于 2013-06-17T21:38:21.383 回答