且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript将富文本内容复制到剪贴板

更新时间:2023-11-02 22:13:46

我搜索了一个星期,终于找到了我的答案!
对于那些希望使用javascript将富文本复制到剪贴板的用户,然后在下面的链接中使用该函数,就像魅力一样。
不需要闪光灯和其他建议:)

使用JavaScript / jquery将图像复制到剪贴板


I need help copying rich text to the clipboard using JavaScript. i have searched around and haven't found anything to suit my specific need.

My JS

function ctrlA1(corp) {
  with(corp) {
  }
  if (document.all) {
    txt = corp.createTextRange()
    txt.execCommand("Copy")
  } else
    setTimeout("window.status=''", 5000)
}

HTML

<div id="sc1">hello <br> <b> world </b> </div>
<button onclick="ctrlA1(document.getElementById('sc1') )"></button>

The above code isn't working and getting an object expected error. any help is appreciated. I have seen a library out there called zeroclipboard, but would prefer to write my own function.

EDIT i now have this function to select text on the page. is it possible to write a formula to copy the selected range as is?

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);
}
}

<label onclick="containerSelect(this); select_all()">
<p>hello world</p>
<img src="imagepath.png">
</label>

i searched for a week now and finally found my answer!!! for those of you looking to copy rich text to the clipboard with javascript, then use the function at the link below, works like a charm. no need of flash and other stuff suggested :)

Copying an image to clipboard using JavaScript/jquery