Detailed explanation of examples of creating Tag tags with JS
This article mainly introduces the method of creating Tag tags with JS, and analyzes the steps and related operating techniques of dynamically operating HTML elements on the page to realize the tag tag function based on specific examples. Friends in need can refer to it. NextThe example in this article describes the method of creating Tag tags with JS. Share it with everyone for your reference, the details are as follows:
1. The principle of creating a label isCreate a node;
var x = document.createElement("TagName")
Assign node style;
x.setAttribute("class",类名)
Assign value to node;
x.innerHTML = 内容 //赋值
Add node to parent element
要添加到的元素.appendChild(x);
2. Style diagram:
3. Main code process: HTML part:
<p class="container"> <h3 id="单击下面得-添加-按钮添加标签">单击下面得"添加"按钮添加标签</h3> <p class="dispanel" id="box"></p> <button class="btn" id="btn1">全部清除</button> <ul id="ul"> <li><span>John Doe1</span> <button class="clibtn btn">添加</button></li> <li><span>John Doe2</span> <button class="clibtn btn">添加</button></li> <li><span>John Doe3</span> <button class="clibtn btn">添加</button></li> <li><span>John Doe4</span> <button class="clibtn btn">添加</button></li> <li><span>John Doe5</span> <button class="clibtn btn">添加</button></li> <li><span>John Doe6 </span><button class="clibtn btn">添加</button></li> </ul> </p>
css part:
body{ margin:0 ; padding:0; background-color:#002F4F; color: #ffffff; font-family: "微软雅黑"; font-size: 1em; } ul,h3{margin: 0; list-style: none; padding: 0px} .container{ width:300px; height:350px; margin: 50px auto; } .dispanel{ width: 290px; height:50px; background-color: #ffffff; margin: 0 auto; } .btn{ width:100px; height:20px; color: #ffffff; background-color:red; border: 0px; font-size: 1em; margin:10px 0 10px 5px; } .container ul li{ width:300px; height:30px; margin-top:10px; } .spanstyle{display: inline-block; color:#000; width:85px;height:22px; background-color: bisque; margin-left:5px; font-size: 12px; text-align: center; line-height: 22px; }
js part:
var oUl = document.getElementById("ul"); var oBtn = oUl.getElementsByClassName("btn"); var oLi = document.getElementsByClassName("li"); var oBox = document.getElementById("box"); for(var i = 0;i<oBtn.length;i++) { oBtn[i].onclick = function () { var oA = document.createElement("span"); //创建一个节点Node var oNew = oA.setAttribute("class", "spanstyle"); //将节点上增加class样式 var ospan = this.previousElementSibling.innerHTML + " X"; //this == oBtn[i] / previousElementSiling:上一个元素的兄弟节点 即 <span> oA.innerHTML = ospan; //将ospa的值付给新创建的节点Node oA. oBox.appendChild(oA); //将oA 添加为oBox的儿子 oA.onclick=function () { oBox.removeChild(oA); //移除这个元素 } } } var obtn1 = document.getElementById("btn1"); obtn1.onclick=function () { oBox.innerHTML=""; //清除内容 }
The above is the detailed content of Detailed explanation of examples of creating Tag tags with JS. For more information, please follow other related articles on the PHP Chinese website!

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
