JS - 为复制的内容增加版权信息
在复制页面内容后提示版权信息及为复制的内容增加版权信息。
$("body").on('copy', function (e) {
if (typeof window.getSelection == "undefined") return; //IE8 or earlier...
var body_element = document.getElementsByTagName('body')[0];
var selection = window.getSelection();
if (("" + selection).length < 30) return;
var newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
body_element.appendChild(newdiv);
newdiv.appendChild(selection.getRangeAt(0).cloneContents());
//提示复制成功代码start
if(window.getSelection){//DOM,FF,Webkit,Chrome,IE10
selection = window.getSelection();
alert("复制成功~\n若转载请注明出处:"+document.location.href);
//document.write("复制成功~\n转载请注明出处:"+document.location.href);
}else if(document.getSelection){//IE10
selection= document.getSelection();
alert("复制成功~\n若转载请注明出处:"+document.location.href);
}else if(document.selection){//IE6+10-
selection= document.selection.createRange().text;
alert("复制成功~\n若转载请注明出处:"+document.location.href);
}else{
selection= "";
alert("浏览器兼容问题导致复制失败!");
}
//提示复制成功代码end
if (selection.getRangeAt(0).commonAncestorContainer.nodeName == "PRE") {
newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
}
newdiv.innerHTML += "</br></br>若转载请注明出处: <a href='" + document.location.href + "'>" + document.location.href + "</a> © xiaowiba.com";
selection.selectAllChildren(newdiv);
window.setTimeout(function () { body_element.removeChild(newdiv); }, 200);
});
复制了您的代码怎么没有反应?
认真核实一下,可能哪里没弄好。