简体   繁体   English

jQuery - 如何按属性选择

[英]jQuery - How to select by attribute

I have a HTML like this, 我有这样的HTML,

<a id="a_1" href="#" disabled_onclick="true">Link1</a>
<a id="a_2" href="#">Link2</a>
<a id="a_3" href="#" disabled_onclick="true">Link3</a>
<input id="b_1" type="submit" disabled_onclick="true">Button1</input>
<input id="b_2" type="submit">Button2</input>
<input id="b_3" type="submit">Button3</input>

Now I need write a jQuery which returns me all the attributes in my html having a disabled_onclick property set to true. 现在我需要编写一个jQuery,它返回我的html中的所有属性,其中disabled_onclick属性设置为true。 In this case, I should get 3 elements, two link tags and one input tag. 在这种情况下,我应该得到3个元素,两个链接标记和一个输入标记。

Here 's how to select all those elements: 以下是如何选择所有这些元素:

$('[disabled_onclick="true"]');

Since true is a valid unquoted attribute value in CSS , you could even omit the quotes : 由于true是CSS中有效的不带引号的属性值 ,您甚至可以省略引号

$('[disabled_onclick=true]');

If you care about valid HTML you should consider using a custom data-* attribute instead though. 如果你关心有效的HTML,你应该考虑使用自定义data-*属性。

<input id="b_1" type="submit" disabled_onclick="true">
<!-- …becomes… -->
<input id="b_1" type="submit" data-disabled-onclick="true">

That way it's valid HTML, and you'll still be able to select it as follows: 这样它就是有效的HTML,你仍然可以按如下方式选择它:

$('[data-disabled-onclick="true"]');

试试这个

$('[disabled_onclick="true"]')
$('input[disabled_onclick="true"]');

See http://api.jquery.com/attribute-equals-selector/ http://api.jquery.com/attribute-equals-selector/

In the above line, you'd only query for input nodes , you can omit the input which would then query over all nodes in your entire markup (which is probably kind of slow'ish). 在上面的行中,您只查询input nodes ,您可以省略input ,然后查询整个标记中的所有节点(这可能有点慢)。

$('[disabled_onclick="true"]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM