search

JS adds new element element

Mar 17, 2018 pm 03:43 PM
javascriptelementnode

This time I will bring you JS to add a new element node. What are the precautions for adding a new element node to JS? The following is a practical case, let's take a look.

nbsp;html>


  <meta>
  <title>www.jb51.net - JS添加新节点的几种方法</title>


<p>
<span>
  1234567890
</span>
</p>

<script>
  //首先找到Id为d的元素
  var d=document.getElementById(&#39;d&#39;);
  //创建一个节点
  var a=document.createElement(&#39;a&#39;);
  //设置a的属性
  a.href=&#39;https://www.baidu.com/&#39;;
  a.innerText=&#39;ggggg&#39;;
  //添加元素  将创建的节点添加到Id为d的p里
  d.appendChild(a);
  //在指定节点前插入新节点
  var p=document.createElement(&#39;p&#39;);
  //添加文本内容
  p.innerText=&#39;ppppppppppppppppp&#39;;
  //d.appendChild(p);
  //参数1:要添加的元素 参数2:要放到哪个节点的前面
  d.insertBefore(p,a);
  //获取目标元素
  var s=document.getElementById(&#39;s&#39;);
  //克隆新元素
  var spanc= s.cloneNode(true);//默认参数是false
  d.appendChild(spanc);
</script>
Screenshot of running effect:

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use the encapsulation function of JS motion buffering effect

Detailed explanation of asynchronous loading of JavaScript

The above is the detailed content of JS adds new element element. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.