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

拈花ヽ惹草 提交于 2019-12-24 15:50:31

问题


Why does only 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>

回答1:


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().




回答2:


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.



来源:https://stackoverflow.com/questions/2687287/why-cant-i-get-jquerys-live-or-load-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!