开发者

Refer to an object inside an $.getJSON() with append

开发者 https://www.devze.com 2022-12-14 11:26 出处:网络
i try to get a json list and append it $.getJSON(\"url\", function(data){ $(\'#tbl\').append(\"开发者_开发问答<li id=\"listitem\">asd</li>);

i try to get a json list and append it

$.getJSON("url",
        function(data){
        $('#tbl').append("开发者_开发问答<li id="listitem">asd</li>);
        });

It works but i cant access the li object with

$("#listitem").hover( alert("Hover"); );

`


This should work

$("#listitem").hover( function() {alert("Hover");} );

hover expects an anonymous function or a callback.


Try with livequery plugin. It should help in these case. Try something like this:

$('#tbl').append("<li id="listitem">asd</li>).livequery( 'hover', doMagic() );


You could also use the .on() method. More information can be found here.

$('#container').on('hover', '#listitem', function(){
     alert("Hover"); 
});

Note: #container should be a parent element that does not change.


Probably you can't access the li element, because you are mixing " in different scopes, so the resulting string is not valid.

$('#tbl').append("<li id="listitem">asd</li>);

The inner " delimiters should be escaped like \"

$('#tbl').append("<li id=\"listitem\">asd</li>);

0

精彩评论

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