是否可以用javascript复制javascript?例如:
<div class="copyThis">
<script language="javascript">
$(function()
{
$("#click").click(function(e){
$('.copyThis').clone().appendTo('#copyArea');
e.preventDefault();
});
});
</script>
CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT
<div style="width: 200px; height: 50px; background-color: gray;">
<a id="click" href="">click here to copy</a>
</div>
<div id="copyArea">
Put here:
</div>
</div>
但这不会复制标签及其内容。至少据我所知并非如此。
注意: 我发现这个问题与我在这里发布的另一个问题有关:Infinite-loop question: Is it possible to make a "Copy this code to share", with a "copy this code to share" inside of it? 我希望可以单独发布这个问题,因为这是一个好奇心。
最佳答案
编辑:我认为这就是你想要的。此( demo )将填充文本区域以便于复制:
<div id="copyThis">
<script language="javascript">
$(function()
{
$("#click").click(function(e){
$('#copyArea').val($('#copyThis').html());
e.preventDefault();
});
});
</script>
CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT
<div style="width: 200px; height: 50px; background-color: gray;">
<a id="click" href="">click here to copy</a>
</div>
<textarea rows="10" cols="40" id="copyArea"></textarea>
</div>
<小时/>
您之前曾进行过 DOM 操作 ready
。使用:
<script language="javascript">
$(function()
{
$("#click").click(function(e){
$('#copyThis').clone().appendTo('#copyArea');
e.preventDefault();
});
});
</script>
参见the demo .
关于javascript - 是否可以用javascript复制javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4455162/