Javascript 是否有可能将所有具有特定值的元素放入任何类型的属性中?
我想获取HTMLDOM中的所有元素,这些元素具有任何具有特定值的属性 比如说-Javascript 是否有可能将所有具有特定值的元素放入任何类型的属性中?,javascript,jquery,jquery-selectors,jquery-attributes,Javascript,Jquery,Jquery Selectors,Jquery Attributes,我想获取HTMLDOM中的所有元素,这些元素具有任何具有特定值的属性 比如说- <ul style="width:150px;" id="RadioButton1"> <li style="width:150px;"><input type="radio" name="RadioButton1"><label>Option 1</label></li> <li style="width:150px;">
<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>
我想获取任何属性中包含RadioButton1的所有元素。意味着我要选择UL和所有三个单选按钮
注意:上面的HTML我使用的是UL-LI结构,但实际情况却太不一样了。所以请不要给出完全支持UL-LI的答案。我想要全球解决方案
是否存在任何选择器来选择所有类型的属性
在这种方法中,是否可以选择具有的元素
提前表示感谢 $“[name=RadioButton1]”将选择所有具有RadioButton1作为其名称属性的项目
$'[id=RadioButton1]'将选择所有具有RadioButton1作为其id属性的项目。请记住,对于ID属性,应该只有一个具有任何给定ID的项
$'[name=RadioButton1],[id=RadioButton1]'将为您提供组合列表
请记住,ID和Name属性都设置为RadioButton1的项目将添加两次
如果要搜索第三个属性,只需将该属性添加到以逗号分隔的选择器列表中即可。要实现此目的,应使用data types=RadioButton 然后使用
$('input[data-types=RadioButton]')
可以通过过滤每个元素并检查其属性来完成:
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);
}
}
});
});
试试这个:
$("*").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
}
});
});
注意:检查控制台中的元素
任何随机选择的属性名称都可以,不是吗?假设它从数据开始-。好的。请看下面的答案,它将帮助您了解全局解决方案的确切含义……@Sadikhasan:我不想使用一个或多个属性选择器。想要选择所有属性类通配符选择器*@DovnVoter:您能告诉我在这里投票的原因吗?谢谢您的支持,但正如我在问题中所述,我正在寻找一个全局解决方案。我不想使用一个或多个属性选择器。想要选择所有属性类通配符选择器*。感谢您的支持,但正如我在问题中所述,我正在寻找一个全局解决方案。引用您的问题:请不要给出完全支持UL-LI的答案。我想要全球解决方案。请解释此答案是如何针对或元素的。感谢您的宝贵帮助:@伊山健:欢迎兄弟:
$(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);
}
}
});
});
$("*").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
}
});
});