Get the App
SLTechnology News&Howtos  ›  Development  › 

How JQuery adds and deletes elements

Shulou Source: shulou.com Published: 2022-06-01 13:11:11 10月27日 Update

This article mainly introduces JQuery how to add elements and delete elements, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Add elements to JQuery, delete elements 1. JQuery create element method let div = $('elm')

$('elm') can create a div element with the content elm

two。 Add element 2.1. Add append (), the original $of the prepend () method (function () {let li = $('added'); $('ul') .append (li);})

The append () method adds the element to the end of the matching element, similar to appendChild () in the native JS.

Original $(function () {let li = $('added'); $('ul') .prepend (li);})

The prepend () method adds the element to the front of the matching element, similar to insertBefore () in the native JS.

When elements are added internally, a parent-child relationship is generated.

2.2. Add before (), after () methods externally

Original

$(function () {let div = $('

Added

'); $(' div') .before (div);})

The before () method adds the element before the matching element

Original $(function () {let div = $('added'); $('div') .after (div);})

The after () method adds the element after the matching element

External elements are added to generate a brotherly relationship

two。 Delete element

1.remove ()

$(function () {$('ul') .remove ();})

Remove () removes the ul element, including child nodes

2.empty ()

$(function () {$('ul') .empty ();})

Empty () clears all child nodes within the ul node, but retains the ul itself

3.html ("")

The effect of html ("") method on empty () is basically the same.

Thank you for reading this article carefully. I hope the article "how to add and delete elements in JQuery" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Elements methods articles nodes post generation consistency value function brothers interests content at the same time more friends father and son knowledge editing industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Shulou Information NVidia MySQL