Home > Web Front-end > JS Tutorial > Usage examples of using jQuery to process page elements_jquery

Usage examples of using jQuery to process page elements_jquery

WBOY
Release: 2016-05-16 16:19:15
Original
1004 people have browsed it

The example in this article describes the use of jQuery to process page elements. Share it with everyone for your reference. The specific analysis is as follows:

For page elements, they can be managed through various query and modification methods in DOM programming, which is very troublesome. jQuery provides a whole set of methods for manipulating page elements. Including the content, copy, move and replacement of elements. Here are some commonly used ones.

1. Obtain and edit content directly.

In jQuery, the content of the page is obtained and edited mainly through the two methods html() and text(). Among them, html() is equivalent to getting the innerHTML attribute of the node. Adding the parameter html(text) sets the innerHtml; text() is used to get the plain text of the element, and text(content) sets the plain text.

These two methods are sometimes used together. text() is used to filter the tags in the page, while html(text) is used to set the innerHtml in the node. For example:

Copy code The code is as follows:
$(function() {
var sString = $("p:first").text(); //Get plain text
$("p:last").html(sString);
});

Use the text() method to get the text of the first P, and then use the html() method to assign it to the last

.

Ingenious use of text() and html() methods.

Copy code The code is as follows:

textparagraphshowexample

One click, two clicks, and three clicks of the mouse can be used to obtain and transfer codes.

2. Move and copy elements

In ordinary DOM, if you want to add an element after an element, you usually use the appendChild() or insertBefore() method of the parent element. In many cases, you need to repeatedly find the position of the node. It's very troublesome. jQuery provides the append() method, which can directly add new sub-elements to an element.

Copy code The code is as follows:

1122...


3344...

In addition to adding html code directly, the append() method can also be used to add fixed nodes, such as

Copy code The code is as follows:
$("p").append($("a"));

This situation will be a little different. If the added

is the only element, then $("a") will be moved to the back of all child elements of the element, and if the target

is Multiple elements, then $("a") will add a child element to each P in the form of copy, while itself remains unchanged. Example: Use the append() method to copy and move elements.

Copy code The code is as follows:

Link 1
Link 2

Text 1


Text 2

The above code sets two hyperlinks for append() calls. For the first hyperlink, add the target $("p"), which has two

elements. For the second hyperlink, add the target as the only

element.

You can see that the first hyperlink is added by copying, and the second hyperlink is added by moving.

In addition, as can be seen from the above, the tag after append() is applied to the style of the target

, while maintaining its own style. This is because append() adds as a sub-tag of

, placing behind all sub-tag (text) nodes of

.

In addition to the append() method, jQuery also provides the appendTo(target) method, which is used to add the target element to the child element of the specified target. Its usage and results are completely similar to append().

Copy code The code is as follows:
$(function() {
$("img:eq(0)").appendTo($("p")); //Add multiple targets


$("img:eq(1)").appendTo($("p:eq(0)")); //The added target is unique


});





For the first photo, my colleague added it to three p tags, and for the second photo, he added it to one P element. From the execution results, we can see that the first photo is copied is added to the three P elements in the form of , while the second picture is added in a mobile way.

Corresponding to the append() and appendTo() methods, JQ also provides prepend() and prependTo() methods. These two methods are to add elements before all child elements of the target, and also follow: copy and move additions in principle.

In addition to the above four methods, Jq also provides before(), insertBefore(), after(), and insertAfter(), which are used to add elements directly before or after a node instead of as a child. Element insertion.

Before() is exactly the same as insertBefore(), and after() and insertAfter() are also exactly the same. Here we take after() as an example

Copy code The code is as follows:

Link 1
Link 2

Content 1


Content 2

As a result of running the above code, you can see that the after() method also follows the principle of moving a single target and copying multiple targets, and is no longer added as a child element. Instead, it is the sibling element immediately following the target element.

3. Delete elements.

In DOM programming, to delete an element, you often use the removeChild() method of the parent element, and jQuery provides the remove() method to delete the element directly.

For example, $("p").remove(); deletes all p element tags in the entire page.

remove() also accepts parameters.

Copy code The code is as follows:

Link 1
Link 2

Content 1


Content 2

In the above code, remove() uses a filter selector, and the P element whose text content contains 1 is deleted.

Although remove() can accept parameters, it is usually recommended to determine the object to be deleted in the selector stage, and then use remove() to delete it all at once. ("p:contains('1')").remove(); The effect is exactly the same, and the effect is consistent with the style of other codes.

In the DOM, if you want to delete all the child elements of an element, you often use a for loop with hasChildNodes() to judge, and use removeChildNode() to delete them one by one. Jquery provides the empty() method to directly delete all child elements. .

Copy code The code is as follows:

Link 1
Link 2

Content 1


Content 2

4. Clone elements.

The second section mentions the copying and moving of elements, but this depends on the number of targets. Many times developers hope that even if there is only one target object, the copy operation can still be performed.

jQuery provides the clone() method to accomplish this task.

Copy code The code is as follows:






The results achieved by the appendTo() method in the previous section are also completed.

In addition, the clone() function also accepts a Boolean value object as a parameter. When the parameter is true, in addition to the clone itself, the time method it carries will be copied together.

Copy code The code is as follows:

The above code clones the button itself when the button is clicked, and clones the click event at the same time. The cloned button also has the function of cloning itself.

I hope this article will be helpful to everyone’s jQuery programming.

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