0


前端-js:知识补充(增删改查)

dom操作

document(文档) object(对象) model

增删改查

1.查找

1.1 getElementById()

** **根据id值获取元素,返回符合条件的第一个对象

  1. <div id="aa">demo</div>
  2. <div id="aa">demo1</div>
  3. <p id="aa">bbb</p>
  4. <h3 id="aa">sss</h3>
  5. <div id="pp" class="aa" ds="sc" style="background-color: red;">demo10</div>
  1. var obj=document.getElementById("aa")
  2. console.log(obj)

以文档打印

  1. console.dir(obj)

以对象打印

对象里的属性:

  1. obj.style.background="red"
  2. obj.style.color="yellow"
  3. obj.style.fontSize="80px"
  4. obj.onclick=function(){
  5. alert(1)
  6. }

null-->从上往下进行,先进行外引到这里,没有id为aa的


1.2.getElementsByClassName()
  1. 根据class值获取元素,返回所有符合条件的对象组成的数组
  1. <div class="aa">demo</div>
  2. <div class="aa">demo1</div>
  3. <p class="aa">bbb</p>
  4. <h3 class="aa">sss</h3>
  1. var arr=document.getElementsByClassName("aa")
  2. console.log(arr)

对象:

  1. arr[0].style.background="red"
  2. arr[0].style.color="yellow"
  3. arr[0].style.fontSize="80px"
  4. arr[0].onclick=function(){
  5. alert(1)
  6. }

1.3.getElementsByTagName()

根据元素名称获取元素,返回所有符合条件的对象组成的数组

  1. var arr=document.getElementsByTagName("div")
  2. console.log(arr)
  3. console.log(arr.length)

1.4.querySelector()

根据选择器获取元素 (包含选择器...) 返回符合条件的第一个对象

  1. var obj=document.querySelector(".aa")
  2. console.log(obj)

1.5.querySelectorAll()
  1. 根据选择器获取元素,返回所有符合条件的对象组成的数组
  1. var arr=document.querySelectorAll(".aa")
  2. console.log(arr)

  1. <p class="cc">ch</p>
  2. <div class="aa">child1
  3. <p class="bb">demo</p>child2
  4. <h3 class="aa">demo2</h3>child3
  5. </div>
  6. <div id="pp" class="aa" ds="sc" style="background-color: red;">demo10</div>
  1. var obj=document.querySelector(".aa")
  2. console.log(obj)
  3. console.log(obj.querySelectorAll("p"))

1.6 通过关系查找

找父亲 parentNode parentElement

找孩子 childNodes children

  1. 第一个孩子 firstChild firstElementChild
  2. 最后一个孩子 lastChild lastElementChild

找上一个元素 previousSibling previousElementSibling

下一个元素 nextSibling nextElementSibling

  1. <p class="cc">ch</p>
  2. <div class="aa">child1
  3. <p class="bb">demo</p>child2
  4. <h3 class="aa">demo2</h3>child3
  5. </div>
  6. <div id="pp" class="aa" ds="sc" style="background-color: red;">demo10</div>

在这里,第一个孩子是child1

  1. var obj=document.querySelector('.aa')
  2. console.log(obj.firstChild)


2.修改


2.1修改内容

innerHTML属性 把修改的内容给当作标签处理

innerText 把修改的内容当做文本处理

value修改input里的值

  1. <div class="aa">demo1</div>
  2. <input type="text" class="aa" value="你好">
  3. <input type="text" class="aa" placeholder="请输入账号">提示的作用
  1. var obj=document.querySelector(".aa")
  2. console.dir(obj)
  3. console.log(obj.innerHTML)
  4. obj.innerHTML="<h1>新内容</h1>"
  5. obj.innerText="<h1>新内容</h1>"
  6. obj.value="aaaaaaa"


  1. obj.innerHTML="<h1>新内容</h1>"


  1. obj.innerText="<h1>新内容</h1>"


2.2 修改属性
原生属性
  1. 对象.属性=值
  1. var obj=document.querySelector(".aa")
  2. console.log(obj.className)
  3. obj.class="demo"
  4. console.log(obj)


自定义属性
  1. setAttribute
  1. obj.setAttribute("hello","helloworld")
  2. console.log(obj.getAttribute("hello"))

2.3修改样式
1.对象.style.属性=值
  1. obj.style.background="red"
  2. obj.style.color="yellow"
  3. obj.style.fontSize="80px"

2 对象.style.cssText
  1. obj.style.cssText="background:red;color:yellow;font-size:60px"

3.通过修改属性,结合css达到修改样式的目的
  1. obj.className="red"

tip:随机
  1. obj.onclick=function(){
  2. var bj=""
  3. for(var i=0;i<6;i++){
  4. bj+=Math.round(Math.random()*9)
  5. }
  6. console.log(bj)
  7. obj.style.background="#"+bj
  8. }


综合:

  1. <h1 class="demo" style="background-color: aquamarine;">
  2. <span>demo</span>
  3. <div>aaaa</div>
  4. </h1>
  5. <ul>
  6. <li>hello</li>
  7. <li>hello</li>
  8. <li class="aa">
  9. <p>demo</p>
  10. <h2>tt</h2>
  11. <a href="#">coa</a>
  12. </li>
  13. <li>hello</li>
  14. <li>hello</li>
  15. <li>hello</li>
  16. </ul>
  1. var arr=document.querySelectorAll("li")
  2. console.log(arr)
  3. arr[0].onclick=function(){
  4. arr[0].style.background="red"
  5. }
  6. arr[1].onclick=function(){
  7. arr[1].style.background="red"
  8. }
  9. arr[2].onclick=function(){
  10. arr[2].style.background="red"
  11. }
  12. arr[3].onclick=function(){
  13. arr[3].style.background="red"
  14. }
  15. arr[4].onclick=function(){
  16. arr[4].style.background="red"
  17. }
  18. arr[5].onclick=function(){
  19. arr[5].style.background="red"
  20. }

等于for循环

  1. for(var i=0;i<arr.length;i++){
  2. // 当点击事件时i已经跳出来循环了
  3. arr[i].onclick=function(){
  4. arr[i].style.background="red"
  5. }
  6. }

加上随机

  1. for(var i=0;i<arr.length;i++){
  2. arr[i].onclick=function(){
  3. var bj=""
  4. for(var i=0;i<6;i++){
  5. bj+=Math.round(Math.random()*9)
  6. }
  7. this.style.background="#"+bj
  8. }
  9. }

3.添加

第一步:创造元素

①创建元素createElement()

②复制元素cloneNode() false表示浅复制,只复制外壳 true表示复制全部

  1. var newNode=document.createElement("h1")
  2. console.log(newNode)
  3. newNode.innerHTML="新添加的元素"
  4. newNode.className="aa"
  5. newNode.style.background="yellow"

  1. var oldNode=document.querySelector("h1")
  2. var newNode=oldNode.cloneNode(true)
  3. console.log(newNode)
第二步:添加元素

①appendChild在子元素最后位置添加

②insertBefore(要插入的元素,插入在哪个节点之前) 在某个子元素前添加

③replaceChild(要插入的元素,要被替换的元素)替换掉元素

  1. var container=document.querySelector(".aa")
  2. var h2=document.querySelector("h2")
  3. console.log(container)
  4. console.log(h2)
  5. container.appendChild(newNode)

  1. container.insertBefore(newNode,h2)

  1. container.replaceChild(newNode,h2)

4.删除

父级元素调用删除的方法

removeChild(要删除的元素)

找父级元素,父级元素调方法

  1. var conntainer=document.querySelector(".aa")
  2. var h2=document.querySelector("h2")
  3. conntainer.removeChild(h2)

或者

  1. h2.parentNode.removeChild(h2)


本文转载自: https://blog.csdn.net/m0_75163045/article/details/142343945
版权归原作者 Lojarro 所有, 如有侵权,请联系我们删除。

“前端-js:知识补充(增删改查)”的评论:

还没有评论