jquery 如何用于检查 <a> 标记上的“禁用”属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10198398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:56:41  来源:igfitidea点击:

How is jquery used to check for the 'disabled' attribute on an <a> tag?

jqueryhtml

提问by coder

I have a disabled='disabled'attribute on an <a>tag. How do I test to see if this attribute is on my tag using jquery? The following code returns undefined. I can see the disabledattribute on the tag in firebug and all other attributes on the anchor return successfully using the same syntax. I realize disabledis a custom attribute for an <a>tag.

disabled='disabled'<a>标签上有一个属性。如何使用 jquery 测试此属性是否在我的标签上?以下代码返回undefined. 我可以看到disabledfirebug 中标签上的属性和锚点上的所有其他属性使用相同的语法成功返回。我意识到disabled<a>标签的自定义属性。

$('#anchorID').attr('disabled');

回答by bardiir

Try

尝试

$('#anchorID').is('[disabled=disabled]')

Will return true if disabled="disabled"is an attribute for the element.

如果disabled="disabled"是元素的属性,则返回 true 。

回答by Ayman Safadi

The new and improved way is to use jQuery's prop()function: http://api.jquery.com/prop/#prop1

新的和改进的方法是使用jQuery的prop()函数:http: //api.jquery.com/prop/#prop1

$('#anchorID').prop("disabled");

$('#anchorID').prop("disabled");

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

属性和特性之间的差异在特定情况下可能很重要。在 jQuery 1.6 之前,.attr() 方法在检索某些属性时有时会考虑属性值,这可能会导致不一致的行为。从 jQuery 1.6 开始, .prop() 方法提供了一种显式检索属性值的方法,而 .attr() 方法检索属性。

回答by iambriansreed

Try:

尝试:

$('#anchorID').prop("disabled");

See:

看:

http://api.jquery.com/prop/#prop1

http://api.jquery.com/prop/#prop1

Updated: +1 to Ayman Safadi's answer.

更新:对 Ayman Safadi 的回答 +1。