开发者

jQuery onload function

I m using the following script to call a onload function but it does not works i开发者_Go百科n IE

("#body").attr({onload : "calFact();"});


If you are using jQuery you could use the ready() function like so:

$(function() {
  callFact();
});

or even more simply, just pass the method to jQuery:

$(callFact);

That will fire as soon as the DOM is available to manipulate, sometimes earlier than onload.


You cannot set onload event handlers on anything other than the window. (Well, you can, but it will have no effect.) If you want calFact to run at the onload event, you should use $(window).load(calFact);. But generally it is more useful to call things at the DOMready event: $(document).ready(calFact); or just $(calFact); for short. The difference is that onload happens when everything loaded (including images, which are slow to load), and DOMready happens when the DOM tree is loaded (but images not necessarily yet).

Also, there is not much point in assigning an id of body to the body when there is only one of it anyway; you can just use $('body').


You'll probably want to do this instead:

$('#body').ready(function(){
    calFact();
});


You should use ready()

More at http://api.jquery.com/ready/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜