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

Comparison of jQuery and JavaScript node insertion methods

高洛峰
Release: 2016-12-06 14:35:59
Original
1318 people have browsed it

2. Insert element:

<div>
<p>面朝大海,春暖花开</p>
</div>
Copy after login

(1), jQuery method

1. Insert inside the node:

Comparison of jQuery and JavaScript node insertion methods

The specific implementation is as follows:

$("div").append("<p>这是append()方法添加的内容</p>");//在div元素下第一个子节点位置插入段落
$("div").prepend("<p>这是prepend()方法添加的内容</p>");//在div元素下最后一个子节点位置插入段落
Copy after login

The following two methods are more It conforms to people's general thinking, but the effect is the same

$("<p>这是appendTo方法添加的内容</p>").appendTo("div");//把段落插到div元素的第一个子节点位置
$("<p>这是prependTo方法添加的内容</p>").prependTo("div");//把段落插到div元素的最后一个子节点位置
Copy after login

​​​​​Comparison of jQuery and JavaScript node insertion methods

2. Insert outside the node:​

Comparison of jQuery and JavaScript node insertion methods

The specific implementation is as follows:​​

$("div").after("<p>这是after()方法添加的内容</p>");//在div元素后面插入段落
$("div").before("<p>这是before()方法添加的内容</p>");//在div元素前面插入段落
Copy after login

​​​​

Comparison of jQuery and JavaScript node insertion methods

3. The appendTo(), prependTo(), insertBefore(), insertAfter() methods have destructive operation characteristics, that is, if you select existing content and insert them into the specified object, the content at the original location will be deleted. . In the following example, the paragraph text contained in the original div element is selected and moved behind the div element. The demonstration is as follows:

$("p").insertAfter("div");


Comparison of jQuery and JavaScript node insertion methods (2) JavaScript method

1. Insert inside the node: appendChild() --- corresponds to jQuery's append( ), insertBefore()---corresponds to prepend() in jQuery

  For specific effects, please see the jQuery method above. . .

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!