Home >Web Front-end >HTML Tutorial >Dynamically generate HTML elements and append attributes to elements
This article mainly introduces about dynamically generating HTML elements and appending attributes to elements. It has certain reference value. Now I share it with you. Friends in need can refer to
The first one: document.createElement()
Create an element, and then use the appendChild()
method to add the element to the specified node;
Add a element:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <p id="main"> <span id="login"></span> </p> </body> <script> var link = document.createElement('a'); link.setAttribute('href','#'); link.setAttribute('id','login'); link.style.color = 'green'; link.innerHTML = '登录'; var main = document.getElementById('main'); main.appendChild(link); </script> </html>
Second: Use innerHTML
to add elements directly to the specified node:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <p id="main"> <span id="login"></span> </p> </body> <script> var link = document.createElement('a'); //使用innerHTML将元素直接添加到指定节点 main.innerHTML = "<a href='#' id='login' style='color: red;'>登录</a>"; </script> </html>
Third: jQuery
Create node
Create DOM object in jQuery, use jQuery’s factory function $()
to complete, the format is as follows:
$(html);
$(html)
will create a DOM object based on the incoming HTML markup string, wrap the DOM object into a jQuery
object and return it.
jQuery Insert the created node into the text using methods such as append()
The methods for inserting nodes in jQuery are:
1.
append( )
: Append content to each matching element2.
appendTo()
: Append all matching elements to the specified element, inverting the regular$(A).append(B)
method, instead of appending B to A, append A to B3.
prepend()
method: Prepend content inside each matching element4.
prependTo()
: Prepend all matching content to the specified element, the same asprpend()
Method reversal5.
after()
Insert content after each matching element6.
insertAfter()
Insert all matching elements Insert after the specified element, inverted with theafter()
method7.
before()
Insert content before each matching element8.
insertBefore()
Inserts each matching element before the specified content, inverted with thebefore()
method
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="jquery-1.11.1.min.js"></script> <script> $(function(){ var $link=$('<a href="#" id="link" style="color:pink">登录</a>'); $('#main').append($link); }) </script> </head> <body> <p id="main"></p> </body> </html>
:
1. Use DOM
//使用createElement创建元素 var dialog = document.createElement('p'); var img = document.createElement('img'); var btn = document.createElement('input'); var content = document.createElement('span'); // 添加class dialog.className = 'dialog'; // 属性 img.src = 'close.gif'; // 样式 btn.style.paddingRight = '10px'; // 文本 span.innerHTML = '您真的要GG吗?'; // 在容器元素中放入其他元素 dialog.appendChild(img); dialog.appendChild(btn); dialog.appendChild(span);
var popContent =[ '<li class="monitory-point-li" indexcode="00000000001310013631">', '<span class="checkbox-unchecked"></span>', '<span class="monitory-text" title="'+name+'">'+formedName+'</span>', '</li>' ].join(' '); $('.document').append(popContent);
<p class="se-preview-section-delimiter"></p>
or use this writing method
var popContent = '<li class="monitory-point-li" indexcode="00000000001310013631">'+ '<span class="checkbox-unchecked"></span>'+ '<span class="monitory-text" title="'+name+'">'+formedName+'</span>'+ '</li>'; $('.document').append(popContent);
Related recommendations:
JQuery operation html element click event detailed explanation
The above is the detailed content of Dynamically generate HTML elements and append attributes to elements. For more information, please follow other related articles on the PHP Chinese website!