多个jquery document.ready在页面上通过ajax

时间:2011-08-29 17:11:48

标签: jquery

我有一个使用jQuery document.ready的页面,在点击按钮时填充div的内容。

div中的内容也使用jquery来创建一些图像的悬停预览(也在这个div中)。但我不能再使用document.ready了。该如何实施?

5 个答案:

答案 0 :(得分:0)

使用回调或功能。或两者兼而有之。

$(document).ready(function(){
    $(element).click(function(){
        alert('bla')
    });
    $(element).hover(function(){
        call_alert('bla');
    });
});
function call_alert(stringer) { alert(stringer) }

答案 1 :(得分:0)

您可以根据需要多次使用document.ready。它们是从上到下执行的。

答案 2 :(得分:0)

如果您执行以下操作,则类似于document.ready

(function(){
    //your code here
    alert('this fired when the dom was ready');
})();

答案 3 :(得分:0)

实际上这个问题实际上与document.ready无关。这只是使得脚本不会被执行,直到dom中的所有内容都被加载到浏览器缓存中。基本上只是创建你的函数来绑定按钮单击然后在其回调时将悬停功能绑定到图像。否则,请查看图像的.live()函数。

答案 4 :(得分:0)

$(document).ready(function(){ })

简单地说,在文档对象的ready事件中执行此匿名函数中的代码,这是在加载dom时触发的事件。

在其中,您可以根据需要为任意数量的事件分配尽可能多的功能。

在加载dom之前解析Javascript,并且所有函数都绑定到浏览器的memmory中的事件。所以,你所说的是将所有这些函数绑定到所有这些事件,但只有在dom准备好之后才能将它保存起来,这样可以节省它寻找尚未解析的元素。

你可以这样做

$(document).ready(function() {
$("h1").click(function() {
alert("Someone clicked h1");
});
$("h2").hover(function() {
alert("someone is hovering our h2s"); //never do this as it will fire endless alerts and crash the browser.
})
})

或者像这样:

function hover_func(el) {
alert("someone is hovering over"+el.tagName);
}

function hover_func(el) {
alert("someone clicked"+el.tagName);
}

$(document).ready(function() {
$("h1").click('click_func($(this))');
$("h2").hover('hover_func($(this))');
})