0

我想将此代码放在我的页面上:

function addLink() {  
    var body_element = document.getElementsByTagName('body')[0];  
    var selection; selection = window.getSelection();  
    var pagelink = "<br /><br /> original: <a href='"+document.location.href+"'>"+document.location.href+"</a>";  
    var copytext = selection + pagelink;  
    var newdiv = document.createElement('div'); newdiv.style.position='absolute';  
    newdiv.style.left='-99999px';  
    body_element.appendChild(newdiv);  
    newdiv.innerHTML = copytext;  
    selection.selectAllChildren(newdiv);  
    window.setTimeout(function() {  
        body_element.removeChild(newdiv);  
    }, 0);
} 
document.oncopy = addLink;

但是这个代码操作适用于所有标签,我不想在“textareas”中操作。

4

1 回答 1

0

测试tagNameaddlink()

document.body.addEventListener('copy', function (e) {
    if (e.target.tagName === 'textarea') {
        return;
    }
    addlink()
}
于 2013-03-14T10:06:58.333 回答