如何在 jQuery 中引用附加项? [英] How do I refer to an appended item in jQuery?

查看:19
本文介绍了如何在 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?

推荐答案

简而言之,$buy 就是你附加的对象,你只是在做一个无效操作.<img/> 是一个自闭合标签,里面没有 HTML,所以 .html("something") 将不起作用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆