Home > Web Front-end > JS Tutorial > Summary of DOM node operation methods in jQuery

Summary of DOM node operation methods in jQuery

小云云
Release: 2018-01-24 15:40:03
Original
1597 people have browsed it

This article mainly introduces the complete method of operating DOM nodes in jQuery. Friends who are interested should take a look at it. I hope it can help everyone better learn how to operate DOM nodes in jQuery.

append(content | fn): Append content to each matching element.

For example: Append some HTML tags to all paragraphs.

<p>I would like to say: </p>
$("p").append("<b>Hello</b>");
[ <p>I would like to say: <b>Hello</b></p> ]
Copy after login

appendTo(): ​​This method reverses the conventional $(A).append(B) operation, that is, it does not append B to A, but appends A to B

For example: Append all paragraphs to the element with ID value foo.

<p>I would like to say: </p>
<p></p><p></p>
$("p").appendTo("p");
<p><p>I would like to say: </p></p>
<p><p>I would like to say: </p></p>
Copy after login

prepend(): Prepend content inside each matching element

For example: Prepend some HTML markup code to all paragraphs.

<p>I would like to say: </p>
$("p").prepend("<b>Hello</b>");
[ <p><b>Hello</b>I would like to say: </p> ]
Copy after login

prependTo(content):

Prepend all matching elements to another, specified element element collection. In fact, using this method reverses the conventional $(A).prepend(B) operation, that is, instead of prepending B to A, A is prepended to B.

For example: Append all paragraphs to the element with ID value foo.

<p>I would like to say: </p><p id="foo"></p>
$("p").prependTo("#foo");
<p id="foo"><p>I would like to say: </p></p>
Copy after login

after(): Insert content after each matching element. The inserted element and the inserted element belong to the same level, non-parent-child relationship

For example: insert some HTML markup code after all paragraphs.

<p>I would like to say: </p>
$("p").after("<b>Hello</b>");
<p>I would like to say: </p><b>Hello</b>
Copy after login

before(): Insert content before each matching element.

For example: Insert some HTML markup code before all paragraphs

<p>I would like to say: </p>
$("p").before("<b>Hello</b>");
[ <b>Hello</b><p>I would like to say: </p> ]
Copy after login

insertAfter(): Insert all matching elements after another, specified element element set. In fact, using this method reverses the conventional $(A).after(B) operation, that is, instead of inserting B after A, insert A after B

For example: put all Paragraphs are inserted after an element. Same as $("#foo").after("p")

<p>I would like to say: </p><p id="foo">Hello</p>
$("p").insertAfter("#foo");
<p id="foo">Hello</p><p>I would like to say: </p>
Copy after login

insertBefore(): Insert all matching elements in front of another, specified element set. In fact, using this method reverses the conventional $(A).before(B) operation, that is, instead of inserting B before A, A is inserted before B.

For example: insert all paragraphs before an element. Same as $("#foo").before("p").

<p id="foo">Hello</p><p>I would like to say: </p>
$("p").insertBefore("#foo");
<p>I would like to say: </p><p id="foo">Hello</p>
Copy after login

wrap(): Wrap all matching elements with structured tags of other elements.

This kind of wrapping is most useful for inserting additional structured markup into the document without destroying the semantic quality of the original document. The principle of this function is to examine the first element provided (which is dynamically generated from the provided HTML markup code) and find the top-level ancestor element in its code structure - this ancestor element is the wrapping element. This function cannot be used when the element in the HTML markup code contains text. Therefore, if you want to add text, you should add it after the package is completed.

For example: DOM element used to wrap the target element

<p class="container">
 <p class="inner">Hello</p>
 <p class="inner">Goodbye</p>
</p>
$('.inner').wrap(function() {
 return '<p class="&#39; + $(this).text() + &#39;" />';
});
<p class="container">
 <p class="Hello">
  <p class="inner">Hello</p>
 </p>
 <p class="Goodbye">
  <p class="inner">Goodbye</p>
 </p>
</p>
Copy after login

unwrap(): This method will remove the parent element of the element. This can quickly cancel the effect of the .wrap() method. Matching elements (and their siblings) replace their parent elements in the DOM structure.

For example: Wrap each paragraph with p with the ID "content"

<p>
  <p>Hello</p>
  <p>cruel</p>
  <p>World</p>
</p>
$("p").unwrap()
<p>Hello</p>
<p>cruel</p>
<p>World</p>
Copy after login

wrapAll(): Wrap all matching elements with a single element

This is different from '.wrap()' 'Wrap each matching element once. This kind of wrapping is most useful for inserting additional structured markup into a document without destroying the semantic quality of the original document. The principle of this function is to examine the first element provided and find the top ancestor element in its code structure - this ancestor element is the wrapping element.

For example: wrap all paragraphs with a generated p

$("p").wrapAll("<p></p>");
Copy after login

or

$("p").wrapAll(document.createElement("p"));
Copy after login

Related recommendations:

Example sharing JQuery selector and DOM node operation exercises

Common methods of inserting inside DOM nodes

Examples to explain the interpretation of HTML tags into DOM nodes

The above is the detailed content of Summary of DOM node operation methods in jQuery. 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