- To create a node, use Jquery's factory function $ (): $ (html); a DOM object will be created based on the passed HTML markup string. Jquery will wrap this DOM object into a Jquery object.
- Note: Dynamically created new element nodes will not be automatically added to the document. Instead, you need to use other methods to insert it into the document.
- When creating a single element, you need to pay attention to closed tags and use the standard xhtml format. For example, to create a <p> element, you can use $ ("< p />") or $ ("< p >< /p >"), but you cannot use $ ("< p >") or $ ("< / p >”) or $ (“p”).
- To create a text node is to write the text content directly when creating an element node. Creating attribute nodes is also created together with element nodes.
Example 1:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>创建节点</title>
<script src="jquery-1.11.3.js"></script>
</head>
<body>
<ul id="fruit">
<li title="pg">苹果</li>
<li title="jz">橘子</li>
<li title="xj">香蕉</li>
</ul>
<script>
$(function(){
/*创建li元素节点,创建文本节点,属性节点*/
var $bl=$("<li title='bl'>菠萝</li>");
/*将新创建的元素节点拼接到父节点上(UL上)*/
$("#fruit").append($bl);
/* $("#fruit").append($("<li title='bl'>菠萝1</li>"));*/
});
</script>
</body>
</html>
Leave your cute little like after reading