我需要帮助使用JavaScript将富文本复制到剪贴板.我四处搜寻,但没有找到适合我特定需求的东西.
function ctrlA1(corp) {
with(corp) {}
if (document.all) {
txt = corp.createTextRange()
txt.execCommand("Copy")
} else
setTimeout("window.status=''", 5000)
}Run Code Online (Sandbox Code Playgroud)
<div id="sc1">hello <br> <b> world </b> </div>
<button onclick="ctrlA1(document.getElementById('sc1') )"></button>Run Code Online (Sandbox Code Playgroud)
上述代码不起作用,导致了object expected error.任何帮助表示赞赏!我在那里看过一个叫做库的库zeroclipboard,但我更喜欢编写自己的函数.
我现在有这个功能来选择页面上的文字.是否可以编写一个公式来按原样复制所选范围?
function containerSelect(id) {
containerUnselect();
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(id);
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(id);
window.getSelection().addRange(range);
}
}Run Code Online (Sandbox Code Playgroud)
<label onclick="containerSelect(this); select_all()">
<p>hello world</p>
<img src="imagepath.png">
</label>Run Code Online (Sandbox Code Playgroud)