简体   繁体   English

是否可以将具有特定值的所有元素放入任何类型的属性中?

[英]Is it possible to get all elements which have a specific value into any type of attribute?

I want to get all elements in an HTML Dom, which have any attribute with the a specific value.我想获取 HTML Dom 中的所有元素,这些元素具有具有特定值的任何属性。

For Example -例如 -

<ul style="width:150px;" id="RadioButton1">
   <li style="width:150px;"><input type="radio" name="RadioButton1"><label>Option 1</label></li>
   <li style="width:150px;"><input type="radio" name="RadioButton1"><label>Option 2</label></li>    
   <li style="width:150px;"><input type="radio" name="RadioButton1"><label>Option 3</label> </li>
</ul>

I want to get all elements which contains "RadioButton1" in any attribute.我想获取任何属性中包含“RadioButton1”的所有元素。 Means i want to select UL and all three RadioButtons .意味着我想选择UL和所有三个RadioButtons

Note: Above HTML i used UL-LI structure, but the real scenario it's too much different.注意:上面的 HTML 使用了UL-LI结构,但实际情况相差太大。 So please don't give answer in completely favor of UL-LI .所以请不要给出完全赞成UL-LI答案。 I want global solution.我想要全球解决方案。

There are any selector exist to select all type of attribute?是否存在任何选择器来选择所有类型的属性?

Is it possible to select elements with in this approach?是否可以在这种方法中选择元素?

Bunch of Thanks in advance...!!!提前致谢...!!!

It can be done by filtering each element and checking its attributes:可以通过过滤每个元素并检查其属性来完成:

Fiddle小提琴

var elements = $('*').filter(function () {
    return $(this.attributes).filter(function(){
        return this.nodeValue == 'RadioButton1'; 
    }).length;
});

Try this:尝试这个:

$("*").each(function() { //check all elements inside DOM
    var elem = $(this);
  $.each(this.attributes, function() {
      if(this.value=='RadioButton1'){
          console.log(elem); //your code goes here. elem is nothing but element 
                               //with value RadioButton1 in any attribute
      }
  });
});

NOTE: check console for elements.注意:检查控制台的元素。

DEMO演示

The attributes property contains them all: attributes 属性包含所有这些:

$(this).each(function() {
  $.each(this.attributes, function() {
    // this.attributes is not a plain object, but an array
    // of attribute nodes, which contain both the name and value
    if(this.specified) {
      if(this.value == 'RadioButton1')
      {
           alert(this.name);
      }
    }
  });
});
$("input[name='RadioButton1'],ul[id='RadioButton1']")

$('[name="RadioButton1"]') will select all items that have "RadioButton1" as their name attribute. $('[name="RadioButton1"]')将选择所有具有“RadioButton1”作为其name属性的项目。

$('[id="RadioButton1"]') will select all items that have "RadioButton1" as their id attribute. $('[id="RadioButton1"]')将选择所有具有“RadioButton1”作为其id属性的项目。 Keep in mind that in case of ID attributes, there should only be one item with any given ID.请记住,对于 ID 属性,应该只有一个具有任何给定 ID 的项目。

$('[name="RadioButton1"],[id="RadioButton1"]') will give you the combined list. $('[name="RadioButton1"],[id="RadioButton1"]')会给你组合列表。

Keep in mind that an item that has both the ID and Name attribute set to "RadioButton1" will be added twice.请记住,将 ID 和 Name 属性都设置为“RadioButton1”的项目将被添加两次。

If there is a third attribute on which you want to search, just add that one to the comma-separated list of selectors.如果您要搜索第三个属性,只需将该属性添加到以逗号分隔的选择器列表中。

To achieve this you should play with data-types="RadioButton"为此,您应该使用 data-types="RadioButton"

And then using然后使用

$('input[data-types=RadioButton]')

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

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