溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JS點擊動態(tài)添加標簽、刪除指定標簽的代碼

發(fā)布時間:2020-08-29 03:53:04 來源:腳本之家 閱讀:901 作者:mrr 欄目:web開發(fā)

1.div標簽

<div id="mDiv3">
 <p>1</p> <button onclick="myFun9()">添加</button>
</div>

2.js

function myFun9() {
 var mDiv3 = document.getElementById("mDiv3"); //獲取組件1
 var eleme = document.createElement("p"); //創(chuàng)建組件2
 var ele_content = document.createTextNode("2");//創(chuàng)建節(jié)點內(nèi)容
 eleme.appendChild(ele_content); // 組件添加節(jié)點
 mDiv3.appendChild(eleme); //組件2加入組件1
 }

==================刪除==============================

<button onclick="myFun10()">刪除</button>
 <div id="mDiv4">
 <p id="p1">1</p>
 <p id="p2">2</p>
 <p id="p3">3</p>
 <p id="p4">4</p>
 <p id="p5">5</p>
 </div>
function myFun10(){
 var parent=document.getElementById("mDiv4");
 var son=document.getElementById("p1");
 parent.removeChild(son);
 }

補充:

下面看下js-動態(tài)生成小圓點(根據(jù)輪播圖圖片張數(shù)動態(tài)生成小圓點)

動態(tài)生成小圓點(根據(jù)輪播圖圖片張數(shù)動態(tài)生成小圓點)

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <style>
  body,ul{
   padding: 0;
   margin: 0;
  }
  ul{
   list-style: none;
  }
  .lunbo{
   width: 730px;
   height: 454px;
   margin: 100px auto;
   overflow: hidden;
   position: relative;
  }
  .circle{
   position: absolute;
   left: 50%;
   margin-left: -50px;
   bottom: 10px;   
  }
  .circle span{
   display: inline-block;
   width: 18px;
   height: 18px;
   background-color: #ccc;
   text-align: center;
   border-radius: 18px;
   cursor: pointer;
   margin-right:10px;
  }
  .circle span.current{
   background-color: yellow;
  }
 </style>
 <script> 
  window.onload = function(){
   function $(id){ return document.getElementById(id); }
   //動態(tài)生成輪播圖小圓點
   var circle = document.createElement("div"); 
   circle.setAttribute("class","circle");
   var lis = document.getElementsByTagName("li");
   for(var i=0; i<lis.length; i++){
    var span = document.createElement("span");
    span.innerHTML = i+1;
    circle.appendChild(span);
   }
   $("scroll").appendChild(circle);
   var spanChildren = circle.children;
   spanChildren[0].setAttribute("class","current");
  }
 </script>
</head>
<body>
 <div class="lunbo" id="scroll">
  <ul id="ul">
   <li><img src="images/11.jpg" alt=""></li>
   <li><img src="images/22.jpg" alt=""></li>
   <li><img src="images/33.jpg" alt=""></li>
   <li><img src="images/44.jpg" alt=""></li>
   <li><img src="images/55.jpg" alt=""></li> 
   <li><img src="images/66.jpg" alt=""></li>
  </ul>
  <!-- <div class="circle">
   <span>1</span>
   <span class="current">2</span>
   <span>3</span>
   <span>4</span>
   <span>5</span>
   <span>6</span>
  </div> -->
 </div>
</body>
</html> 

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI