1. 程式人生 > >點選複製 div或span的內容

點選複製 div或span的內容

<span class="dtable" onclick="execClick();" oncopy="execCopy(event,this);"> 123456 </span>


<script>

    function execClick(){
        document.execCommand("copy");
    }

    function execCopy(event,thisDiv){
        if(isIE()){
            if(window.clipboardData){
                window.clipboardData.setData("Text", thisDiv.innerText);
                alert(window.clipboardData.getData("Text"));
            }
        }else{
            event.preventDefault();
            if (event.clipboardData) {
                event.clipboardData.setData("text/plain", thisDiv.innerText);
                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;
        //"!window.document.body.dataset" is faster but the body is null when the DOM is not
        //ready. Anyway, an input tag needs to be created to check if IE is being
        //emulated
        if (!input.dataset) return 10;
        return 11;
    }
</script>