1.遍歷某element元素得到屬性時,屬性包含的信息有不少來自原型鏈上的屬性和方法,而這些信息有時候咱們是不須要的。用下面的方法只得到咱們手寫在標籤上的本身指定的信息。javascript
2.得到咱們手寫在標籤上的值:html
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
<script type="text/javascript" src="jquery-1.6.js"></script>
</head>
<body>
<input type="checkbox" name="cbx_check" id="c1" value="v1" p1="p1" p2="p2" p3="p3"/>
<script type="text/javascript"> java
function getElementAttrObj(elementObj) {
var elementAttrObj = {};jquery
if(elementObj){
var el = elementObj;
var atts = el.attributes;
var el_specified_key_val_str = '';
for (var i =0,len = atts.length; i < len; i++) {
var att = atts[i];
//console.info(att.specified+"-"+att.name + '-' + att.value );
debugger;
var att_name = att.name.toLowerCase();
//規定屬性
if (att.specified || att_name == 'value') {
el_specified_key_val_str += ',"' + att.name + '":"' + att.value + '"';
}
}
//屬性對象
if(el_specified_key_val_str){
el_specified_key_val_str = el_specified_key_val_str.substr(1);
var el_specified_str = "{";
el_specified_str += el_specified_key_val_str;
el_specified_str += "}";
elementAttrObj = eval('(' + el_specified_str + ')');
//console.info(el_specified_key_val_str);
}
}this
return elementAttrObj;
} debug
var el = document.getElementById("c1");
var elementAttrObj = getElementAttrObj(el);
var str = "";
for(p in elementAttrObj){
str += p + ":" + elementAttrObj[p] + ",";
}
alert(str); htm
//jquery的方法
/*
$("#c1").each(function () {
var arr = [];
var attrs = $(this).get(0).attributes;
for (i = 0; i < attrs.length; i++) {
att = attrs[i];
if (att.specified && att.name.toLowerCase().indexOf("data-") != -1) {
arr.push(att.name + ":" + att.value);
$(this).get(0).removeAttribute(att.name);
}
}
var str = arr.join("");
alert(str);
})
*/
</script> 對象
</body>
</html>
ip