Blogger Information
Blog 40
fans 0
comment 1
visits 34271
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jQuery初学习之dom操作
景云
Original
479 people have browsed it

jQuery初学习

使用JQuery前必须先引入库

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>

1. dom操作

html

  1. <ul id="one">
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li>item3</li>
  5. </ul>
  6. <ul id="two">
  7. <li>item1</li>
  8. <li>item2</li>
  9. <li>item3</li>
  10. </ul>

html图

1.1 添加元素

1.1.1 append(与html()功能一样):父元素.append(子元素)

  1. $("body").append("<h2>hello~</h2>");

1.1.1 图

  1. // >后面可以使用函数
  2. $("body").append("<ol></ol>");
  3. $("ol").append(()=>{
  4. let str="";
  5. for(let i=1;i<5;i++){
  6. str+="<li>"+ "这是第"+ i +"个新的商品" + "</li>";
  7. }
  8. return str;
  9. });



1.1.2 appendTo:子元素.appendTo(父元素)

$("<h1>Title</h1>").appendTo($("body"));

1.1.2 图


1.1.2 appendTo:子元素.appendTo(父元素)

$("<h1>Title</h1>").appendTo($("body"));

1.2 filter():过滤器

  1. console.log($("ul").filter("#one"));//获取id为one的ul
  2. console.log($("ul").filter("#one").children());//获取id为one的ul的所以子元素
  3. console.log(document.querySelector("#one").children);//原生写法
  4. // 获取第一个元素
  5. console.log($("ul").filter("#one").children().first().text());//item1 jq写法
  6. console.log(document.querySelector("#one").firstElementChild.textContent);//item1 原生写法
  7. //获取最后一个元素
  8. console.log($("ul").filter("#one").children().last().text());//item3 jq写法
  9. console.log(document.querySelector("#one").lastElementChild.textContent);//item3 原生写法
  10. //获取第n个元素
  11. console.log($("ul").filter("#one").children().eq(1).text());//item2 jq写法
  12. console.log(document.querySelector("#one").children.item(1).textContent);//item2 原生写法
  13. console.log(document.querySelector("#one").children[1].textContent);//item2 原生写法

1.3 find():获取所有层级的元素;children():获取子元素集合

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!