在开发中我们有时会对html标签添加属性,如何遍历处理
<ul>
<li name=
"li1"
sortid=
"nav_1"
>aaaaaaa</li>
<li name=
"li1"
sortid=
"nav_2"
>bbbbbbb</li>
<li name=
"li1"
sortid=
"nav_3"
>cccccccc</li>
<li name=
"li1"
sortid=
"nav_4"
>ddddddd</li>
<li name=
"li1"
sortid=
"nav_5"
>eeeeeee</li>
<li name=
"li1"
sortid=
"nav_6"
>fffffffffffffffff</li>
<li name=
"li1"
sortid=
"nav_7"
>gggggggg</li>
<li name=
"li1"
sortid=
"nav_8"
>hhhhhhhh</li>
<li name=
"li1"
sortid=
"nav_9"
>iiiiiiiiiiiiiiiiiii</li>
</ul>
//以sortid来遍历
$(
"li[sortid^='nav_']"
).each(
function
(i){
var
sortid=$(
this
).attr(
"sortid"
);
});
//以name来遍历
$(
"[name=li1]"
).each(
function
(a,b){
var
sortid= $(b).attr(
"sortid"
);
});
//如果已知某个sortid要取对象
$(
"[sortid='nav_1']"
).attr(
"html"
);
$(
"[sortid='nav_1']"
).attr(
"name"
);
其他:
//查找名字是clssId的文本对象
$(
"input[name='clssId']"
).val();
//查找id有一定规律的,例如查找dd标签中id=single_xxx
$(
"dd[id^=single_]"
).each(
function
(){
var
id = $(
this
).attr(
"id"
);
alert(id.substring(7));
});
以上这篇jquery遍历标签中自定义的属性方法