Home >Web Front-end >JS Tutorial >How Can I Use appendTo() and prependTo() to Move HTML Elements?

How Can I Use appendTo() and prependTo() to Move HTML Elements?

DDD
DDDOriginal
2024-12-06 08:15:16652browse

How Can I Use appendTo() and prependTo() to Move HTML Elements?

Moving an Element Within Another Element in HTML

To move one DIV element inside another, use either the appendTo or prependTo functions.

appendTo:

This function appends the source element to the end of the destination element.

$("#source").appendTo("#destination");

prependTo:

This function adds the source element to the beginning of the destination element.

$("#source").prependTo("#destination");

Example:

<div>

JavaScript:

$("#appendTo").click(function() {
  $("#moveMeIntoMain").appendTo($("#main"));
});
$("#prependTo").click(function() {
  $("#moveMeIntoMain").prependTo($("#main"));
});

HTML:

<div>

The above is the detailed content of How Can I Use appendTo() and prependTo() to Move HTML Elements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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