如何我指的是jQuery的一个附加项目? [英] How do I refer to an appended item in jQuery?
本文介绍了如何我指的是jQuery的一个附加项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要创建和jQuery中添加一个项目,同时节省对它的引用。
I want to create and append 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?
推荐答案
在短, $购买
的是的对象你追加,你只是在做无效操作。 &LT; IMG /&GT;
是一个自闭的标签,里面有它没有HTML,这样的 的.html(东西)
不会有任何效果。
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.
如果你的意思是设置提示,使用 .attr()
,像这样的:
If you meant to set the tooltip, use .attr()
, like this:
$buy.attr('alt', 'hello');
这篇关于如何我指的是jQuery的一个附加项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文