1. 程式人生 > >js讀取剪貼簿內容

js讀取剪貼簿內容

注:該讀取剪貼簿方法存在瀏覽器相容問題,適用於ie,不適用於firefox、google,報錯Uncaught ReferenceError: clipboardData is not defined

以下是讀取剪貼簿內容的js程式碼:

<script>
    var text = window.clipboardData.getData("Text");
	alert(text);
</script>

以下是將文字寫入剪貼簿的例子:

<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script	src="https://cdn.jsdelivr.net/clipboard.js/1.5.12/clipboard.min.js"></script>
</head>
<body>
    <p>哈哈
        <span id="target">你沒看錯,就是複製的這裡</span>
	</p>
	<button class="btn" data-clipboard-action="copy"
		data-clipboard-target="#target" id="copy_btn">複製按鈕</button>
</body>

<script>    
    $(document).ready(function(){
        var targetText=$("#target").text();    
        var clipboard = new Clipboard('#copy_btn');    
    
        clipboard.on('success', function(e) {    
            console.info('Action:', e.action);    
            console.info('Text:', e.text);    
            console.info('Trigger:', e.trigger);    
            alert("複製成功");    
    
            e.clearSelection();    
        });    
    });    
</script>