下面的代码正在删除它找到的第一个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); (或至少,所讨论的区域如下所示)
<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为“通知”)的div。
我认为罪魁祸首在于守则的这一节:
jQuery('#" + this.id + "').hide(); 发布于 2013-06-17 21:34:47
用这个代替
(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-17 21:38:21
你用了两次相同的身份证。按照规则,HTML元素的id应该是唯一的。
当你使用这个:jQuery('#" + this.id + "').hide(); "this.id“指的是”通知“。但是它隐藏了第一个div,因为它是用id="notify"呈现的第一个div,所以它首先找到了这个div。
尝试使用唯一的ids:
<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>https://stackoverflow.com/questions/17157198
复制相似问题