开发者

Why can't I get jQuery's live() or load() to work?

开发者 https://www.devze.com 2022-12-28 05:39 出处:网络
Why does onl开发者_StackOverflow社区y the third method work? $(\'#jqtest\').live(\'load\', function() {$(this).html(\'hi\');}); //1

Why does onl开发者_StackOverflow社区y the third method work?

$('#jqtest').live('load', function() {$(this).html('hi');}); //1

$('#jqtest').load(function() {$(this).html('hi');}); //2

$(window).load(function() {$('#jqtest').html('hi');}); //3


 <div id="jqtest">kldjfglkj</div>


You can't use the load() function on arbitrary selectors; you can only use it on "any element associated with a URL: images, scripts, frames, iframes, and the window object" (docs). divs don't have an associated URL, so neither of your first two techniques will bind a handler. window does have a URL, so it will call the handler.

You might also also be interested in ready().


If you're trying to add the HTML "hi" to the element "#jqtest" when the document or window has loaded you're almost there.

$(document).ready(function(){

$("#jqtest").html('hi');

});

This will change the value of "#jqtest" when the document has been loaded. You can also specify other events within the ready() function to only be executed once the page has fully loaded.

0

精彩评论

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

关注公众号