Home > Web Front-end > JS Tutorial > body text

How to dynamically create tags and set attributes using js

php中世界最好的语言
Release: 2018-03-17 13:44:57
Original
2570 people have browsed it

This time I will bring you the method of dynamically creating tags and setting attributes with js. What are the precautions for dynamically creating tags with js and setting attributes. The following is a practical case. Get up and take a look.

When we write jsp pages, we often encounter this situation: the number of data obtained from the background is uncertain. At this time, when writing jsp pages on the front end, we are not sure how to design it. At this time, you need to dynamically create tags through js:

1. Create a certain tag: Create an instance of p in the body as follows;

<script>
 function fun(){
 var framep = document.createElement("p");//创建一个标签
 var bodyFa = document.getElementById("bodyid");//通过id号获取framep 的父类(也就是上一级的节点)
 bodyFa .appendChild(framep);//把创建的节点framep 添加到父类body 中;
 }
<script>
<body id="bodyid" >
<!--在此添加p标签-->
</body>
Copy after login

2. Add attributes: Add corresponding attributes to the created tag:

framep .setAttribute("id", "pid");//给创建的p设置id值;
framep .className="pclass"; //给创建的p设置class;
//给某个标签添加显示的值;
var h = document.createElement("h1");
h.innerHTML = data[i].name;
var p = document.createElement("p");
p.innerHTML = "要显示的值";
Copy after login

3. Add events to the created tag:

a. Without parameters:

framep.onmousedown = fun;//ps:函数名fun后面一定不能带括号,否则会在创建标签的时候执行函数, 而不是鼠标按下时执行;
Copy after login

b. With parameters:

framep.onmousedown = function(){ fun(this); }
Copy after login

c. The function to be called;

function fun(){ 
alert("鼠标按下");
}
Copy after login

4. If you are worried that the created tag will not be overwritten, you can replace it:

 var pFlag = document.getElementById("pFlag");
 var pMain = document.createElement("p");
 if(pFlag != null){
 body.replaceChild(pMain, pFlag);//把原来的替换掉
}
pMain.setAttribute("id", "pFlag");
Copy after login

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:

D3.js draws dynamic progress bar

##ngrok+express for local environment WeChat interface debugging

What should I do if the dynamically loaded data slide always fails?

The above is the detailed content of How to dynamically create tags and set attributes using js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!