复制变量 <div> 的 html 元素以及其他元素

这是一个<div>用作markdown 到 html 转换器的函数,其内部html是可变的:


<div id="markdown" class="classname">

</div>

我想制作一个 javascript 函数,它复制(到剪贴板)内部html的<div>,以及div: 的样式表(注意:样式表不是可变的)


<style>

html {

font-size: 13px; 

line-height: 1.5; 

padding:60px;

}

 <!-- and many more... -->

</style>

预期的复制结果如下;


<style>

html {

font-size: 13px; 

line-height: 1.5; 

padding:60px;

}

 <!-- and many more... -->

</style>

<!-- below are sample contents from the <div> -->

<h1>markdown test</h1>

<p>hello</p>

需要什么脚本来实现这个?通过窗口警报复制结果prompt也可以。


MM们
浏览 116回答 2
2回答

慕的地8271018

您可以在这里使用 jQuery,使用下面的脚本,您可以在其中阅读HTMLmarkdowndiv和 css 脚本$(function(){&nbsp; var html = $('#markdown').html();&nbsp; console.log(html);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="markdown" class="classname">&nbsp; <style>&nbsp; &nbsp; html {&nbsp; &nbsp; &nbsp; font-size: 13px;&nbsp; &nbsp; &nbsp; line-height: 1.5;&nbsp; &nbsp; &nbsp; padding: 60px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <!-- and many more... -->&nbsp; </style>&nbsp; <!-- below are sample contents from the <div> -->&nbsp; <h1>markdown test</h1>&nbsp; <p>hello</p></div>

杨__羊羊

通过使用clone()jquery 的功能,您可以复制 html 并将其附加到另一个元素中。前任。:<div class="container">&nbsp; <div class="goodbye">&nbsp; &nbsp; Goodbye&nbsp; &nbsp; <div class="hello">Hello</div>&nbsp; </div></div>$( ".hello" ).html();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript