Refer to an object inside an $.getJSON() with append
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>);
精彩评论