English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In development, we sometimes add attributes to HTML tags, how to traverse and process them?
<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> //Traverse by sortid $("li[sortid^='nav_']").each(function(i){ var sortid=$(this).attr("sortid"); }); //Traverse by name $("[name=li1']).each(function(a,b){ var sortid= $(b).attr("sortid"); }); //If a certain sortid is known to take an object $("[sortid='nav_1']).attr("html"); $("[sortid='nav_1']).attr("name");
Other:
//Search for the text object with the name clssId $("input[name='clssId']").val(); //Search for IDs with certain regularities, such as searching for dd tags with id=single_xxx $("dd[id^=single_]").each(function(){ var id = $(this).attr("id"); alert(id.substring(7)); });
This article about the method of traversing custom attributes of tags in jQuery is all the editor shares with everyone. Hope it can be a reference for everyone, and I also hope everyone will support and cheer for the tutorial.