开发者

How do I refer to an appended item in jQuery?

I want to create and appe开发者_JAVA百科nd an item in jquery, while saving a reference to it.

 var buy = "<img src='img/buy-now.png' />";      
 var $buy = $(buy).appendTo("body");
 $buy.html("hello");

I was expecting something like the above to work. Any ideas?


In short, $buy is the object you've appended, you're just doing an invalid operation. <img /> is a self-closing tag, there is no HTML inside it, so .html("something") will have no effect.

If you meant to set the tooltip, use .attr(), like this:

$buy.attr('alt', 'hello');


Yes, the variable $buy will save the reference of the element and the jQuery object.

Edit: $buy.html("hello"); will add "hello" inside the image tag, however it will be hidden on the screen since the image is being displayed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜