1

我正在尝试(没有成功)在 div 中使用“remove”类访问并隐藏所有嵌套元素

这是附加元素的代码片段

$('<li id="com'+msg[key]['id']+'"></li>').prependTo('.mCSB_container').html('<div class="acomments"> <img src="'+url_Avatar+'" alt="Avatar fotomontaggio" /></div><div class="tcomments"><h1>'+nome+'</h1><p class="text">'+commento+'</p><p class="differenza">'+dif+'</p>**<p class="remove" id="r'+msg[key]['id']+'">rimuovi commento</p></div>');

这是试图隐藏这些元素的脚本

$(document).ready(function() { $('.remove').hide();});

有人可以帮我吗?我无法摆脱这个...

4

1 回答 1

0

假设你有

$(function() {
    ...
    $('<li id="com'+msg[key]['id']+'"></li>').prependTo('.mCSB_container').html('<div class="acomments"> <img src="'+url_Avatar+'" alt="Avatar fotomontaggio" /></div><div class="tcomments"><h1>'+nome+'</h1><p class="text">'+commento+'</p><p class="differenza">'+dif+'</p>**<p class="remove" id="r'+msg[key]['id']+'">rimuovi commento</p></div>');

    ...
});

$(document).ready(function() { $('.remove').hide();});

隐藏代码在添加之前运行(在加载之前准备好)。这意味着它什么也没有隐藏。

解决方案是确保在创建元素后执行隐藏代码:

$(function() {
    ...
    $('<li id="com'+msg[key]['id']+'"></li>').prependTo('.mCSB_container').html('<div class="acomments"> <img src="'+url_Avatar+'" alt="Avatar fotomontaggio" /></div><div class="tcomments"><h1>'+nome+'</h1><p class="text">'+commento+'</p><p class="differenza">'+dif+'</p>**<p class="remove" id="r'+msg[key]['id']+'">rimuovi commento</p></div>');

    ...
    $('.remove').hide();
});
于 2012-10-18T12:56:40.053 回答