JavaScript将富文本内容复制到剪贴板 [英] javascript copy rich text contents to clipboard

查看:361
本文介绍了JavaScript将富文本内容复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JavaScript将富文本复制到剪贴板。我搜查了周围,并没有发现任何东西,以满足我的具体需要。



我的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取代;世界< / b> < / DIV>
< button onclick =ctrlA1(document.getElementById('sc1'))>< / button>

以上代码无法正常工作并且收到对象预期错误。任何帮助表示赞赏。
我看到一个名为 zeroclipboard 的库,但宁愿写我自己的函数。



编辑
i现在可以使用此功能选择页面上的文本。是否可以编写一个公式来复制所选范围?

 函数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>


解决方案

我搜索了一个星期,终于找到了我的答案!
对于那些希望使用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

这篇关于JavaScript将富文本内容复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆