简体   繁体   English

追加后的jQuery选择器不起作用

[英]jQuery selector after append not working

I am trying to append dynamic data to my page. 我正在尝试将动态数据添加到页面中。

I have 我有

<div id="container"> ... </div>

and want to add content to it 并想要添加内容

$('#container').append('<p id="myid" style="display:none">Test</p>');

Then I try to do 然后我尝试去做

$('#myid').fadeIn();

and it doesn't work. 而且不起作用。 No errors or anything are printed. 没有错误或任何打印。 The append works but the fadeIn doesn't do anything as I think selector is not able to get the newly added element. 追加有效,但是fadeIn不会执行任何操作,因为我认为选择器无法获取新添加的元素。

I know that if I wanted to have dynamic event listener for the newly created element, I would need to use jQuery's on but in this case I am not sure how to re-use the selector. 我知道,如果我想为新创建的元素提供动态事件侦听器,则需要使用jQuery的on,但是在这种情况下,我不确定如何重用选择器。

您需要在#container周围加撇号:

$(#container)

试试这个:

$('#container').find('#myid').fadeIn();

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

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