简体   繁体   English

选择所有具有特定属性值的首个元素

[英]Select all first elements with specific attribute value

On a form, I have to select all first <a> elements with a specific attribute value from all the list items present in the form. 在表单上,​​我必须从表单中存在的所有列表项中选择具有特定属性值的所有第一个<a>元素。 For instance, in the below code, I have to select <a> elements with the content xxx and zzz — or in other words, the first <a> element with data-role="modal" from all list items. 例如,在下面的代码,我必须选择<a>与内容要素xxxzzz -或者换句话说,第一<a>与要素data-role="modal"从所有列表项。

 console.log($('ul li a[data-role="modal"]:first')); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <a data-role="modal">xxx</a> <a data-role="modal">yyy</a> </li> <li> <a>abc</a> <a data-role="modal">zzz</a> <a data-role="modal">xyz</a> </li> </ul> 

The jQuery I tried is only selecting the one with xxx . 我尝试过的jQuery只选择了xxx

You can use .find() to get all a elements with attribute data-role=modal in ul li and apply :eq to get first for each match: 您可以使用.find()获取ul li所有属性为data-role=modal a元素,并应用:eq获得每个匹配项的第一位:

 $('ul li').find('a[data-role="modal"]:eq(0)').css('color', 'red'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <a data-role="modal">xxx</a> <a data-role="modal">yyy</a> </li> <li> <a>abc</a> <a data-role="modal">zzz</a> <a data-role="modal">xyz</a> </li> </ul> 

References 参考

.find() 。找()

:eq :EQ

Try: 尝试:

$('ul li').find('a[data-role="modal"]:first')

 console.log( $('ul li').find('a[data-role="modal"]:first').map(function() { return $(this).text(); }).get() ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li> <a data-role="modal">xxx</a> <a data-role="modal">yyy</a> </li> <li> <a>abc</a> <a data-role="modal">zzz</a> <a data-role="modal">xyz</a> </li> </ul> 

Check this 检查一下

$('document').ready(function(){
    $('ul li').each(function(){
     $(this).find('a[data-role=modal]:first').css('background-color','red');
    });
});

I guess this may help 我想这可能会有所帮助

$('document').ready(function(){
    $('ul li').each(function(){
     $(this).find('a[data-role="modal"]').first().css('background-color','red');
    });
});

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

相关问题 选择具有设置为特定值的属性的元素内的所有元素 - Select all the elements within an element having an attribute set to a specific value 选择所有具有特定数据属性Javascript的元素? - Select ALL elements with specific data attribute Javascript? JavaScript-选择[data-some]属性没有特定值的所有元素 - JavaScript - Select all elements with [data-some] attribute not having a specific value jQuery选择器存储在变量中:如何通过属性值选择特定元素? - jQuery selector stored in variable: How to Select specific elements by attribute value? jQuery-选择所有以...开头的属性名称(不是值)的元素? - jQuery - select all elements with attribute name (not value) beginning with…? Select 具有特定值并随 Javascript 更改其值的所有输入元素 - Select all input elements that have a specific value and change their value with Javascript 选择具有特定属性的Web表单元素 - Select web form elements with specific attribute 如何使用jquery查找并选择具有以特定单词开头的数据属性的所有元素? - How can I find and select all elements which have data attribute start with specific word with using jquery? 删除所有不具有特定属性的元素 - Delete all elements that do not possess specific attribute 一般选择所有数据属性元素 - Select all data attribute elements generically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM