jQuery复制HTML元素到文本区域

4
<ins id="widget"
     style=""
     data-lang="fr"
     data-city-id="669"
     data-font-color="000000"
     data-background-color="ffffff"
     data-border-color="c0c0c0"
     data-datetime-color="000000"
     data-input-background="ffffff"
     data-next-background="d8d8d8">
</ins>
<textarea id="the-code"></textarea>
<button type="button" id="get-code">Get the code</button>

我正在尝试使用Jquery将ins元素克隆到textarea中。
$("#get-code").on("click", function () {
    var clone = $("#widget").clone();
    $("#the-code").val(clone);
});

克隆的内容在文本框中返回[object Object]而不是HTML代码< ins >

示例:FIDDLE


1
@GhostCat 好的,已经注意到了! - Yanga
非常感谢您的友好和快速回复! - GhostCat
1个回答

6

There is no need to clone the element just get the outerHTML property of dom object.

$("#get-code").on("click", function() {
  $("#the-code").val($("#widget")[0].outerHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ins id="widget" style="" data-lang="fr" data-city-id="669" data-font-color="000000" data-background-color="ffffff" data-border-color="c0c0c0" data-datetime-color="000000" data-input-background="ffffff" data-next-background="d8d8d8">
    </ins>
<textarea id="the-code"></textarea>
<button type="button" id="get-code">Get the code</button>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接