我知道以下 jquery 语法将为 src 设置属性值
$(this).closest('img.button').attr('src', '/images/button.png');
但是如何获取或获取 'src' 属性的值以在我的 jQuery 脚本中使用?
同样的方法,只是不提供第二个参数。
$(this).closest('img.button').attr('src');
如果您只提供一个参数,.attr(...)
则充当getter。
如果您提供两个参数,则.attr(...)
充当setter。
如果您想稍后删除该值,您将执行以下操作:
$(this).closest('img.button').removeAttr('src');
这是 jQuery 文档的链接$.attr()
。
attr 是 getter 和 setter 函数。此处的文档:http: //api.jquery.com/attr/
获取属性:
$(this).closest('img.button').attr('src');
设置属性:
$(this).closest('img.button').attr('src', '/images/button.png');