jQuery - 如何通过属性进行选择 [英] jQuery - How to select by attribute

查看:63
本文介绍了jQuery - 如何通过属性进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的HTML,

 < a id =a_1href =#disabled_onclick =真>链接1< / A> 
< a id =a_2href =#> Link2< / a>
< a id =a_3href =#disabled_onclick =true> Link3< / a>
< input id =b_1type =submitdisabled_onclick =true> Button1< / input>
< input id =b_2type =submit> Button2< / input>
< input id =b_3type =submit> Button3< / input>

现在我需要编写一个jQuery,它将我的html中的所有属性都设置为disabled_onclick属性真正。在这种情况下,我应该得到3个元素,两个链接标记和一个输入标记。

这里是如何选择所有这些元素的:

  $( '[disabled_onclick = 真]'); 

由于 true 是CSS中有效的未加引号属性值,您甚至可以
$ $ $ $''[disabled_onclick =真正]');

如果您关心有效的HTML,您应该考虑使用自定义的 data- *

 < input id =b_1type =submitdisabled_onclick = 真 > 
<! - ...变成... - >
< input id =b_1type =submitdata-disabled-onclick =true>

这样,它就是有效的HTML,您仍然可以按照如下方式选择它:

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


I have a HTML like this,

<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. In this case, I should get 3 elements, two link tags and one input tag.

解决方案

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:

$('[disabled_onclick=true]');

If you care about valid HTML you should consider using a custom data-* attribute instead though.

<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:

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

这篇关于jQuery - 如何通过属性进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆