如何向页面加载后创建的元素添加内容
我有动态创建的 div,我想在加载时向其中添加一些元素。 jQuery .load()
和 .live()
没有帮助我。
$('div#mydiv').load(function(){
$(this).html('loaded!');
});
I have div that created dynamically, and I want to add some elements into it when its loaded. jQuery .load()
and .live()
didn't help me.
$('div#mydiv').load(function(){
$(this).html('loaded!');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在就绪部分创建一个单击事件:
You should make an event for click in ready section:
如果没有更多背景信息,很难知道问题出在哪里。如果可以的话,请发布您想要执行的操作的更完整的片段。
从您到目前为止发布的内容来看,我认为
#myDiv
div 不存在于页面的初始 HTML 中。所以我看到两种可能性:$('#foo').append('
')< /code> 然后,只要您的代码将其附加到页面中的某个元素,该元素就会可用,因此在这种情况下,假设元素
#foo
已经在 DOM 中,你可以做点什么就像下一行的$('#myDiv').text('Loaded!')
一样,.load
与元素 ($('# foo').load('/someurl')
)您可以添加另一个带有回调函数的参数,如果您使用的是$.ajax
或其简写朋友($.get
、$.post
等),以及附加/创建内容动态地从其成功
或完整
回调,请参阅前面的要点。希望这有帮助。由于没有更多上下文,很难对您的问题给出更具体的答案,抱歉!
It's very hard to know what the problem is without more context. If you can, post a more complete snippet of what you're trying to do.
From what you've posted so far, I take it the
#myDiv
div is not present in the initial HTML of the page. So I see two possibilities:$('#foo').append('<div id="myDiv"></div>')
. Then the element will be available and 'there' as soon as your code appends it to some element in the page. So in this case, assuming element#foo
is already in the DOM, you could do something like$('#myDiv').text('Loaded!')
on the next line..load
with an element ($('#foo').load('/someurl')
) you can add another parameter with a callback function which will be called once the content has been loaded. If you're using$.ajax
or its shorthand friends ($.get
,$.post
, etc.), and appending/creating content dynamically from itssuccess
orcomplete
callbacks, see the previous bullet point.Hope this helps. Without more context, it's hard to give a more specific answer to your question, sorry!
尝试加载外部文件,然后在回调中插入代码:
如果一切都失败,请尝试使用 $.Ajax 加载文件,并使用完整的处理程序插入内容。
Try loading the external file and then inserting your code in the callback:
If all else fails, try using $.Ajax to load the file, and the complete handler to insert your content.