简体   繁体   English

jQuery从div复制html并粘贴到输入中

[英]jquery copy html from div and paste into a input

i am trying to copy the raw html with a div called #copy_history. 我正在尝试使用名为#copy_history的div复制原始html。 I have managed to do it with the text works fine but i require the html as well. 我设法做到这一点与文本工作正常,但我也需要html。

The following does work but its grabbing the text not all the html: 下面的方法确实有效,但是它抓取的文本不是所有的html:

script: 脚本:

$('#copy').click(function() {
    var text = $('#copy_history').text();
    $('.paste_history').val(text);
});

See html and/or clone : 参见html和/或clone

Grab the markup inside a given element: 给定元素获取标记:

 $('#copy').on('click', function() { $('#input').val($('#sample').html()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="sample">Here's some <b>markup</b></div> <label for="input">Input</label> <input type="text" id="input"/> <button id="copy">Copy sample markup</button> 

Grab the markup inside and including the given element: 在内部包含给定元素的标记:

 $('#copy').on('click', function() { $('#input').val($('<div/>').append($('#sample').clone()).html()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="sample">Here's some <b>markup</b></div> <label for="input">Input</label> <input type="text" id="input"/> <button id="copy">Copy sample markup</button> 

$('#copy').click(function() {
    var text = $('#copy_history').html();
    $('.paste_history').val(text);
});

Hope this will work.. 希望这会工作..

Try append() insteal of val() : 尝试对val() append()窃取:

$('#copy').click(function() {
    var text = $('#copy_history');
    $('.paste_history').append(text);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM