国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery常見的遍歷DOM操作詳解

2019-11-19 13:01:38
字體:
來源:轉載
供稿:網友

本文實例總結了jQuery常見的遍歷DOM操作。分享給大家供大家參考,具體如下:

向上遍歷DOM樹

  • .parent():返回被選元素的直接父元素,該方法只會向上一級對DOM樹進行遍歷
  • .parents():返回被選元素的所有祖先元素,一直向上遍歷,直到文檔的根元素(html)
  • .parentsUntil():返回介于兩個給定元素之間的所有祖先元素
<!DOCTYPE html><html><head><style>.ancestors *{display:block;border:2px solid lightgrey;color:lightgrey;padding:5px;margin:15px;}</style><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script >$(document).ready(function(){$("span").parent().css({"color":"red","border":"2px solid red"});});</script></head><body><div class="ancestors"><div style="width:500;">div(曾祖父)<ul>ul(祖父)<li>li(直接父)<span>span</span></li></ul></div><div style="width:500px;">div(祖父)<p>p(直接父)<span>span</span></p></div></div></body></html>

運行結果:

parentsUntil()方法

$(document).ready(function(){$("span").parentsUntil("div");});
<!DOCTYPE html><html><head><style>.ancestors *{display:block;border:2px solid lightgrey;color:lightgrey;padding:5px;margin:15px;}</style><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("span").parentsUntil("div").css({"color":"red","border":"2px solid red"});});</script></head><body class="ancestors">body(增曾祖父)<div style="width:500px;">div(曾祖父)<ul>ul(祖父)<li>li(直接父)<span>span</span></li></ul></div></body></html>

運行結果:

向下遍歷DOM樹

  • .children():返回被選元素的所有直接子元素,該方法只會向下一級對DOM樹進行遍歷
  • .find():返回被選元素的后代元素,一直向下直到最后一個后代

children()方法

<!DOCTYPE html><html><head><style>.descendants *{display:block;border:2px solid lightgrey;color:lightgrey;padding:5px;margin:15px;}</style><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("div").children().css({"color":"red","border":"2px solid red"});$("div").children("p.1").css({"color":"red","border":"2px solid red"});});</script></head><body><div class="descendants" style="width:500px;">div(當前元素)<p class="1">p(子)<span>span(孫)</span></p><p class="2">p(子)<span>span(孫)</span></p></div></body></html>

運行結果:

find()方法

<!DOCTYPE html><html><head><style>.descendants *{display:block;border:2px solid lightgrey;color:lightgrey;padding:5px;margin:15px;}</style><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("div").find("span").css({"color":"red","border":"2px solid red"});});</script></head><body><div class="descendants" style="width:500px;">div(current element)<p>P子<span>span(孫)</span></p><p>p子<span>span(孫)</span></p></div></body></html>

運行結果:

返回<div>所有后代

$(document).ready(function(){$("div").find("*");});

水平遍歷DOM樹

  • .siblings():返回被選元素的所有同胞
  • .next():返回被選元素下一個同胞元素
  • .nextAll():返回被選元素的所有跟隨的同胞元素
  • .nextUntil():返回介于兩個給定參數之間的所有跟隨的同胞元素
  • .prev():返回被選元素上一個同胞元素
  • .prevAll():返回被選元素的所有之前的同胞元素
  • .prevUntil():返回介于兩個給定參數之間的所有之前的同胞元素
<!DOCTYPE html><html><head><style>.siblings *{display:block;border:2px solid lightgrey;color:lightgrey;padding:5px;margin:15px;}</style><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("h2").siblings().css({"color":"red","border":"2px solid red"});});</script></head><body class="siblings"><div>div(父)<p>p</p><span>span</span><h2>h2</h2><h3>h3</h3><p>p</p></div></body></html>

運行結果:

jQuery遍歷 過濾

  • first()方法:返回被選元素的首個元素
  • last()方法:返回被選元素的最后一個元素
  • eq()方法:返回被選元素中帶有指定索引號的元素
  • filter()方法:允許自己規定一個標準,不匹配這個標準的元素會被從集合中刪除,匹配的元素會被返回。
  • not()方法:返回不匹配的所有元素
<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("div p").first().css("background-color","yellow");});</script></head><body><h1>我心在北朝、</h1><div><p>田野上</p></div><div><p>紅彤彤的野花</p></div><p>玲瓏剔透</p></body></html>

運行結果:

eq()方法的使用

<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("p").eq(1).css("background-color","yellow");});</script></head><body><h1>我心在南朝、</h1><p>田野上</p><p>紅彤彤的野花</p><p>玲瓏剔透</p><p>我愛你</p></body></html>

運行結果:

filter()方法的使用

<!DOCTYPE html><html><head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>$(document).ready(function(){$("p").filter(".intro").css("background-color","yellow");});</script></head><body><h1>我心在南朝、</p><p>田野上</p><p class="intro">紅彤彤的草莓</p><p class="intro">玲玲剔透</p><p>我愛你</p></body></html>

運行結果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼運行效果。

更多關于jQuery相關內容還可查看本站專題:《jQuery操作DOM節點方法總結》、《jQuery遍歷算法與技巧總結》、《jQuery表格(table)操作技巧匯總》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》、《jquery選擇器用法總結》及《jQuery常用插件及用法總結

希望本文所述對大家jQuery程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浮山县| 阿坝| 崇礼县| 玛沁县| 惠东县| 揭阳市| 龙南县| 巴楚县| 集贤县| 濮阳市| 乐山市| 丘北县| 宁晋县| 梁山县| 清丰县| 额济纳旗| 昆山市| 梅河口市| 沁阳市| 华阴市| 鄢陵县| 商南县| 通榆县| 湛江市| 杭锦后旗| 兴安盟| 大冶市| 田阳县| 乌审旗| 瑞昌市| 宿州市| 耒阳市| 永新县| 深水埗区| 卓资县| 绍兴县| 昂仁县| 娄烦县| 丰县| 池州市| 丰台区|