遍历同辈节电的方法_jQuery遍历
一。遍歷子元素
children()方法可以用來獲取元素的所有子元素
語法:$(selector).children([expr]);
示例:
var $section =$("section").children();
alert($section.length);
二。遍歷同輩元素
jQuery可以獲取緊鄰其后、緊鄰其前和位于該元素前與后的所有同輩元素
語法:
next([expr])
用于獲取緊鄰匹配元素之后的元素
$("li:eq(1)").next().addClass("orange");
prev([expr])
用于獲取緊鄰匹配元素之前的元素
$("li:eq(1)").prev().addClass("orange");
slibings([expr])
用于獲取位于匹配元素前面和后面的所有同輩元素
$("li:eq(1)").siblings().addClass("orange");
三。遍歷前輩元素
parent():獲取元素的父級元素
parents():獲取元素的祖先元素
實例:
$("li:eq(1)").parent().addClass("orange");
$("li:eq(1)").parents().addClass("orange");
四,其他遍歷方法1
each( ) :規定為每個匹配元素規定運行的函數
語法:?$(selector).each(function(index,element)) ;
示例
$("img").click(function(){
$("li").each(function(){
var str=$(this).text()+"
";
$("section").append(str);
})
});
其他遍歷方法2
end( ):結束當前鏈條中的最近的篩選操作,并將匹配元素集還原為之前的狀態
實例:
$(".contain :header").css({"background":"#2a65ba","color":"#ffffff"});
$(".gameList li").first().css("background","#b8e7f9").end().last().css ("background","#d3f4b5");
$(".gameList li:last").css("border","none");
除css()外,還有獲取和設置元素高度、寬度等的樣式操作方法
總結
以上是生活随笔為你收集整理的遍历同辈节电的方法_jQuery遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python的数组属性_[Python]
- 下一篇: virtuoso从电路图导入版图_基于V