[英]How can I clone element and append to the clone another el?
我有一些帶有元素(類和ID)的id div,我需要對其進行克隆並將其追加到克隆的新類中。 我該怎么做?
window.onload = Func (); function Func () { var temp = document.getElementById("start"); var cl = temp.cloneNode(true); var div = document.createElement('div'); div.className = "class"; div.innerHTML = "MyText"; var result = cl.getElementById("second").append(div); alert(result.innerHTML); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="start"> <div id="second"> <a href="#">Link</a> </div></div>
使用以下代碼
$(function(){
var $Div = $('.start').clone();
$('.second').html($Div);
});
使用JQuery,
var clone = $("#divName");
or var clone = $(".className")
var secondDiv = $("#secondDiv");
or var secondDiv = $(".secondDivClassName");
//Or you can get the html of your second div like this:
var myHtml = secondDiv.html(); //if you don't give any parameter, it takes the inner html of the given element.
//and finally insert your html into the clone like this :
clone.html(myHtml);// in this case, the html() methode takes a parameter and inserts the given parameters into the given element.
您應該將JQuery引用到您的頁面。 告訴我是否可行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.