如何使用jQuery获取HTML标记的属性值

时间:2013-02-26 17:21:48

标签: jquery attr

我知道以下jquery语法将设置src的属性值

$(this).closest('img.button').attr('src', '/images/button.png');

但是如何获取或获取'src'属性的值以便在我的jQuery脚本中使用?

2 个答案:

答案 0 :(得分:4)

同样的方法,就是不要提供第二个参数。

$(this).closest('img.button').attr('src');

如果您只提供一个参数,.attr(...)将充当 getter

如果您提供两个参数,那么.attr(...)将充当 setter


如果您想删除该值,稍后您将执行以下操作:

$(this).closest('img.button').removeAttr('src');

以下是$.attr()的jQuery文档的链接。

答案 1 :(得分:1)

attr是getter和setter函数。文档:http://api.jquery.com/attr/

获取属性:

$(this).closest('img.button').attr('src');

设置属性:

$(this).closest('img.button').attr('src', '/images/button.png');
相关问题