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

Summary of DOM node operation methods in jQuery

小云云
Release: 2018-01-24 15:40:03
Original
1449 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.

I would like to say: 

$("p").append("Hello"); [ 

I would like to say: Hello

 ]
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.

I would like to say: 

$("p").appendTo("p");

I would like to say: 

I would like to say: 

Copy after login

prepend(): Prepend content inside each matching element

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

I would like to say: 

$("p").prepend("Hello"); [ 

HelloI would like to say: 

 ]
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.

I would like to say: 

$("p").prependTo("#foo");

I would like to say: 

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.

I would like to say: 

$("p").after("Hello");

I would like to say: 

Hello
Copy after login

before(): Insert content before each matching element.

For example: Insert some HTML markup code before all paragraphs

I would like to say: 

$("p").before("Hello"); [ Hello

I would like to say: 

 ]
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")

I would like to say: 

Hello

$("p").insertAfter("#foo");

Hello

I would like to say: 

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").

Hello

I would like to say: 

$("p").insertBefore("#foo");

I would like to say: 

Hello

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

 

Hello

 

Goodbye

$('.inner').wrap(function() {  return '

'; });

 

  

Hello

 

 

  

Goodbye

 

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"

  

Hello

  

cruel

  

World

$("p").unwrap()

Hello

cruel

World

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("

");
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!