javascript 如何在 jquery 中刷新准备好的文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5462879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how refresh the document ready in jquery
提问by mkotwd
for example if i write this code :
例如,如果我编写此代码:
$(document).ready(function(){
$("div#i").click(function(){
alert("ok");
});
});
and the div element with the i id is not found in the page when load , so after i create it with some function ; and i click it , nothing happen , so how i can refresh the ready function in jquery ..
加载时在页面中找不到带有 i id 的 div 元素,所以在我用一些函数创建它之后;我点击它,什么也没发生,所以我如何刷新 jquery 中的就绪函数..
回答by Loktar
回答by Justin Thomas
Look into using .live()
: http://api.jquery.com/live/
考虑使用.live()
:http: //api.jquery.com/live/
回答by LesterDove
Take that click event handler out of (document).ready and place it in the function that you've mentioned which creates the ID.
从 (document).ready 中取出单击事件处理程序,并将其放置在您提到的创建 ID 的函数中。
function someFunction()
{
//creates the id, then
$("div#i").click(function(){
alert("ok");
});
}
EDIT:
编辑:
If this is just one example among many potential event handlers, then wrap them all in a single function, say, bindEvents()
and you can call that every time the page is 'dirtied' by an ID creation.
如果这只是许多潜在事件处理程序中的一个示例,那么将它们全部包装在一个函数中,例如,bindEvents()
您可以在每次页面因 ID 创建“弄脏”时调用该函数。
But the other commenters' approach of using .live() will probably be less to maintain, and it's "the JQuery way" if that's a concern for you.
但是其他评论者使用 .live() 的方法可能不太容易维护,如果您担心这就是“JQuery 方式”。