点击复制文字

通过代码功能实现点击文字即可复制的功能。

代码示例

<script type='text/javascript'> 
function execClick(){
    document.execCommand('copy');
}

function execCopy(event,thisDiv){
    if(isIE()){
        if(window.clipboardData){
            window.clipboardData.setData('Text', thisDiv.textContent);
            alert(window.clipboardData.getData('Text'));
        }
    }else{
        event.preventDefault();
        if (event.clipboardData) {
            event.clipboardData.setData('text/plain', thisDiv.textContent);
            alert(event.clipboardData.getData('text'));
        }
    }
}

function isIE(){
    var input = window.document.createElement ('input');
    /*'!window.ActiveXObject' is evaluated to true in IE11*/
    if (window.ActiveXObject === undefined) return null;
    if (!window.XMLHttpRequest) return 6;
    if (!window.document.querySelector) return 7;
    if (!window.document.addEventListener) return 8;
    if (!window.atob) return 9;
    if (!input.dataset) return 10;
    return 11;
}
</script> 

调用代码

<div style="text-align:center;color:#e8e8e8;font-size:10px;" id="thisDiv" onclick="execClick();" oncopy="execCopy(event,this);">要复制的文字</div>