Javascript 使用jQuery获取元素的所有属性
我试图遍历一个元素并获取该元素的所有属性以输出它们,例如,一个标记可能有3个或更多属性,我不知道,我需要获取这些属性的名称和值。我的想法大致如下:Javascript 使用jQuery获取元素的所有属性,javascript,jquery,attributes,Javascript,Jquery,Attributes,我试图遍历一个元素并获取该元素的所有属性以输出它们,例如,一个标记可能有3个或更多属性,我不知道,我需要获取这些属性的名称和值。我的想法大致如下: $(this).attr().each(函数(索引,元素){ 变量名称=$(this).name; var值=$(this).value; //使用名称和值执行某些操作。。。 }); 有人能告诉我这是否可能,如果可能,正确的语法是什么吗?属性包含所有这些属性: $(this).each(function() { $.each(this.attr
$(this).attr().each(函数(索引,元素){
变量名称=$(this).name;
var值=$(this).value;
//使用名称和值执行某些操作。。。
});
有人能告诉我这是否可能,如果可能,正确的语法是什么吗?属性
包含所有这些属性:
$(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) {
console.log(this.name, this.value);
}
});
});
您还可以扩展.attr
,这样就可以像调用.attr()
一样调用它,以获得所有属性的普通对象:
(function(old) {
$.fn.attr = function() {
if(arguments.length === 0) {
if(this.length === 0) {
return null;
}
var obj = {};
$.each(this[0].attributes, function() {
if(this.specified) {
obj[this.name] = this.value;
}
});
return obj;
}
return old.apply(this, arguments);
};
})($.fn.attr);
用法:
var $div = $("<div data-a='1' id='b'>");
$div.attr(); // { "data-a": "1", "id": "b" }
var$div=$(“”);
$div.attr();//{“数据-a”:“1”,“id”:“b”}
使用LoDash,您只需执行以下操作:
_.transform(this.attributes, function (result, item) {
item.specified && (result[item.name] = item.value);
}, {});
这里概述了许多可以实现的方法,供我和你参考:)函数返回属性名及其值的散列
香草JS:
function getAttributes ( node ) {
var i,
attributeNodes = node.attributes,
length = attributeNodes.length,
attrs = {};
for ( i = 0; i < length; i++ ) attrs[attributeNodes[i].name] = attributeNodes[i].value;
return attrs;
}
jQuery
此函数需要一个jQuery对象,而不是DOM元素
function getAttributes ( $node ) {
var attrs = {};
$.each( $node[0].attributes, function ( index, attribute ) {
attrs[attribute.name] = attribute.value;
} );
return attrs;
}
下划线
也适用于lodash
function getAttributes ( node ) {
return _.reduce( node.attributes, function ( attrs, attribute ) {
attrs[attribute.name] = attribute.value;
return attrs;
}, {} );
}
lodash
比下划线版本更简洁,但仅适用于lodash,不适用于下划线。需要IE9+,在IE8中有bug。感谢@AlJey
测试页面
在JS Bin,有一个覆盖所有这些功能的应用程序。该测试包括布尔属性(hidden
)和枚举属性(contenteditable=“
)。使用javascript函数,更容易以NameDarray格式获取元素的所有属性
$(“#myTestDiv”)。单击(函数(){
var attrs=document.getElementById(“myTestDiv”).attributes;
$.each(属性、函数(i、元素){
$(“#attrs”).html($(“#attrs”).html()+”
“+elem.name+”:“+elem.value+”);
});
});代码>
点击这个
属性是调试脚本(基于hashchange上面的答案的jquery解决方案)
下划线.js的简单解决方案
例如:获取所有链接文本谁的父母有班级someClass
_.pluck($('.someClass').find('a'), 'text');
我的建议:
$.fn.attrs = function (fnc) {
var obj = {};
$.each(this[0].attributes, function() {
if(this.name == 'value') return; // Avoid someone (optional)
if(this.specified) obj[this.name] = this.value;
});
return obj;
}
var a=$(el.attrs() 这是给你的一行
JQuery用户:
用jQuery对象替换$jQueryObject
。i、 e$('div')
香草Javascript用户:
用HTML DOM选择器替换$domeElement
。i、 edocument.getElementById('demo')
干杯 当没有匹配的元素时,您可能需要修复它,例如$().attr()
属性集合包含旧IE中所有可能的属性,而不仅仅是HTML中指定的属性。您可以通过使用指定的每个属性过滤属性列表来解决这个问题。这是jQuery.attr()
方法的一个非常好的预期功能。奇怪的是,jQuery没有包含它。只是有点好奇,为什么我们要在[0]中以数组的形式访问它。属性
?属性
不是数组。。。在Chrome中,它至少是一个名为nodeMap的对象。
function getAttributes ( $node ) {
$.each( $node[0].attributes, function ( index, attribute ) {
console.log(attribute.name+':'+attribute.value);
} );
}
getAttributes($(this)); // find out what attributes are available
_.pluck($('.someClass').find('a'), 'text');
$.fn.attrs = function (fnc) {
var obj = {};
$.each(this[0].attributes, function() {
if(this.name == 'value') return; // Avoid someone (optional)
if(this.specified) obj[this.name] = this.value;
});
return obj;
}
Object.values($jQueryObject.get(0).attributes).map(attr => console.log(`${attr.name + ' : ' + attr.value}`));
Object.values($domElement.attributes).map(attr => console.log(`${attr.name + ' : ' + attr.value}`));