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

Detailed explanation of jQuery.clone() function example

巴扎黑
Release: 2018-05-19 16:16:18
Original
1950 people have browsed it

The

clone() function is used to clone a copy of the current matching element collection and returns it in the form of a jQuery object. You can also simply understand it as: clone the current jQuery object.

You can also specify whether to copy additional data (data() function) of these matching elements (even their child elements) and bind events.

This function belongs to the jQuery object (instance).

Syntax

jQueryObject.clone( withDataAndEvents [, deepWithDataAndEvents ] )

Parameters

Parameter Description

withDataAndEvents Optional/Boolean type Whether to copy the element's additional data and binding events at the same time, the default is false.

deepWithDataAndEvents Optional/Boolean type Whether to copy the additional data and binding events of all sub-elements of the element at the same time, the default value is the value of the parameter withDataAndEvents.

clone() is mainly used to clone a copy of the current jQuery object.

jQuery 1.5 New support: clone() supports the second parameter deepWithDataAndEvents. This parameter indicates whether to copy additional data and binding events for all child elements of the cloned element at the same time.

Note:

1. Before jQuery 1.4, the clone() function only additionally copied the binding event of the element. Starting from version 1.4, it began to support copying additional data of the element.

2. In version 1.5.0 (only 1.5.0), the default value of the parameter withDataAndEvents was incorrectly set to true. Starting from 1.5.1, its default value was changed back to false.

Return value

clone()The return value of the function is of jQuery type and returns a clone copy of the current jQuery object.

Note: For performance reasons, the clone() function will not copy the dynamics of some form elements, such as the content entered by the user in