相思资源网 Design By www.200059.com

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"); //创建组件2
 var ele_content = document.createTextNode("2");//创建节点内容
 eleme.appendChild(ele_content); // 组件添加节点
 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-动态生成小圆点(根据轮播图图片张数动态生成小圆点)

动态生成小圆点(根据轮播图图片张数动态生成小圆点)

<!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); }
   //动态生成轮播图小圆点
   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="/UploadFiles/2021-04-02/11.jpg">

标签:
js,删除指定标签,js,添加标签

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“JS点击动态添加标签、删除指定标签的代码”

暂无JS点击动态添加标签、删除指定标签的代码的评论...