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

时间:2022-11-19 15:25:23

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 disabled attribute on the tag in firebug and all other attributes on the anchor return successfully using the same syntax. I realize disabled is a custom attribute for an <a> tag.

我在一个< >标记上有一个disabled='disabled'属性。如何使用jquery测试这个属性是否在标记上?下面的代码返回未定义的。我可以看到在firebug中标记上的禁用属性以及锚点返回的所有其他属性都使用相同的语法成功地返回。我知道disabled是标记的自定义属性。

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

3 个解决方案

#1


39  

Try

试一试

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

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

如果禁用=“禁用”,将返回true,这是元素的属性。

#2


37  

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(“禁用”);

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()则检索属性。

#3


5  

Try:

试一试:

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

See:

看到的:

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

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

Updated: +1 to Ayman Safadi's answer.

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

#1


39  

Try

试一试

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

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

如果禁用=“禁用”,将返回true,这是元素的属性。

#2


37  

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(“禁用”);

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()则检索属性。

#3


5  

Try:

试一试:

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

See:

看到的:

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

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

Updated: +1 to Ayman Safadi's answer.

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