-
Notifications
You must be signed in to change notification settings - Fork 5
/
copyzhihu.html
34 lines (34 loc) · 1.83 KB
/
copyzhihu.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<div id="answer">测试数据<b>test</b></div>
copy事件可以参考:
https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/oncopy
http://codebits.glennjones.net/editing/setclipboarddata.htm
<script>
function setClipboardText(event){
event.preventDefault();
var node = document.createElement('div');
//对documentfragment不熟,不知道怎么获取里面的内容,用了一个比较笨的方式
node.appendChild(window.getSelection().getRangeAt(0).cloneContents());
var htmlData = '<div>著作权归作者所有。<br />'
+ '商业转载请联系作者获得授权,非商业转载请注明出处。<br />'
+ '作者:tiantian<br />链接:http://segmentfault.com/u/tiantian_<br />'
+ '来源:segmentfault<br /><br />'
+ node.innerHTML
+ '</div>';
var textData = '著作权归作者所有。\n'
+ '商业转载请联系作者获得授权,非商业转载请注明出处。\n'
+ '作者:tiantian\n链接:http://segmentfault.com/u/tiantian_\n'
+ '来源:segmentfault\n\n'
+ window.getSelection().getRangeAt(0);
if(event.clipboardData){
event.clipboardData.setData("text/html", htmlData);
event.clipboardData.setData("text/plain",textData);
}
else if(window.clipboardData){
return window.clipboardData.setData("text", textData);
}
};
var answer = document.getElementById("answer");
answer.addEventListener('copy',function(e){
setClipboardText(e);
});
</script>