2010-08-09 62 views
0

我正在一個網站上,客戶在產品頁面上有優惠券代碼圖片,當用戶點擊圖片時,他希望代碼複製到剪貼板......這可能嗎?將圖像數據複製到剪貼板?

+0

代碼或圖像? – 2010-08-09 21:11:47

回答

0

JavaScript剪貼板訪問在所有瀏覽器中都被禁用,除非用戶在其設置中啓用它。解決方法是使用閃存複製到剪貼板腳本,但Flash獲得更多的安全限制。檢查出zeroclipboard

0

如果你想要的形象被複制,您可以使用「createControlRange()」,例如:IE以外

var imageTag = document.getElementById("couponCode"); 
if (document.body.createControlRange) //to check if browser is IE 
{ 
    var couponRange; 
    document.getElementById("couponCode").contentEditable = true; 
    couponRange = document.body.createControlRange(); 
    couponRange.addElement(imageTag); 
    couponRange.execCommand('Copy'); 
    document.getElementById("couponCode").contentEditable = false; 
    alert("Image copied"); 
} 
else //for other browsers 
{ 
    alert("Your browser does not allow access to clipboard.\nPlease press Ctrl+C to copy"); 
    var couponRange = document.createRange(); 
    couponRange.selectNode(imageTag); 
    window.getSelection().removeAllRanges(); 
    window.getSelection().addRange(couponRange); 
} 

瀏覽器不允許JavaScript訪問剪貼板出於安全原因。但是您可以通過要求用戶在選擇優惠券圖像範圍後按Ctrl + C來調用瀏覽器功能。

希望這可以解決您的問題。