脚本宝典收集整理的这篇文章主要介绍了js實例教程-javascript用createRange拷貝Range片段到粘貼板(代碼實例),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小寶典致力於為廣大程序猿(媛)提供高品質的代碼服務,請大家多多光顧小站,小寶典在此謝過。
最近項目需要做一個編輯器,其中有個按鈕點擊可以將編輯好的文章一鍵複製到粘貼板,產品的要求是複製好的文章能連帶圖片和樣式一起粘貼到word中。本來打算用clipboard.js來做的,但發現它只能複製文本。
在谷歌之後,有了新的方案:利用Range對象選擇需要複製的範圍,然後將選好的範圍複製下來。
我用layui的define方法封裝了一個對象
layui.define(['jquery','layer'],function(@R_512_995@s){ VAR $=layui.jquery var layer=layui.layer var OBJ={ __isSupport:function(){ var supportsRange=document.implementation.hasFeature("Range","2.0"); var alsoSupportsRange=(tyPEof document.createRange == 'function') var supportsSelection=(typeof window.getSelection == 'function') var supportsExecCommand=(typeof document.execCommand == 'function') var result=supportsRange && alsoSupportsRange && supportsSelection && supportsExecCommand; if(!result){ layer.alert('您的瀏覽器版本不支持範圍複製,請手動複製或換高版本瀏覽器') } return result; }, __simple:function(selector,type){ var self=this; var node,range,selection; if(!self.__isSupport()){ return; } node=$(selector)[0] range=document.createRange() if(type=='node'){ range.selectNode(node); }else if(type=="nodeContents"){ range.selectNodeContents(node) } selection=window.getSelection() selection.empty(); selection.addRange(range); document.execCommand('Copy') selection.empty(); range.detach() range=null }, //拷貝節點 node:function(selector){ var self=this; self.__simple(selector,'node') }, //拷貝節點的內容 nodeContents:function(selector){ var self=this; self.__simple(selector,'nodeContents') } } exports('copy',OBJ) })
這個是需要依賴layui的,後面有空擼個不依賴任何js庫的。
最近項目需要做一個編輯器,其中有個按鈕點擊可以將編輯好的文章一鍵複製到粘貼板,產品的要求是複製好的文章能連帶圖片和樣式一起粘貼到word中。本來打算用clipboard.js來做的,但發現它只能複製文本。
在谷歌之後,有了新的方案:利用Range對象選擇需要複製的範圍,然後將選好的範圍複製下來。
我用layui的define方法封裝了一個對象
layui.define(['jquery','layer'],function(exports){ var $=layui.jquery var layer=layui.layer var OBJ={ __isSupport:function(){ var supportsRange=document.implementation.hasFeature("Range","2.0"); var alsoSupportsRange=(typeof document.createRange == 'function') var supportsSelection=(typeof window.getSelection == 'function') var supportsExecCommand=(typeof document.execCommand == 'function') var result=supportsRange && alsoSupportsRange && supportsSelection && supportsExecCommand; if(!result){ layer.alert('您的瀏覽器版本不支持範圍複製,請手動複製或換高版本瀏覽器') } return result; }, __simple:function(selector,type){ var self=this; var node,range,selection; if(!self.__isSupport()){ return; } node=$(selector)[0] range=document.createRange() if(type=='node'){ range.selectNode(node); }else if(type=="nodeContents"){ range.selectNodeContents(node) } selection=window.getSelection() selection.empty(); selection.addRange(range); document.execCommand('Copy') selection.empty(); range.detach() range=null }, //拷貝節點 node:function(selector){ var self=this; self.__simple(selector,'node') }, //拷貝節點的內容 nodeContents:function(selector){ var self=this; self.__simple(selector,'nodeContents') } } exports('copy',OBJ) })
這個是需要依賴layui的,後面有空擼個不依賴任何js庫的。
覺得可用,就經常來吧!Javascript技巧 腳本寶典 歡迎評論哦! js技巧,巧奪天工,精雕玉琢。小寶典獻醜了!
以上是脚本宝典为你收集整理的js實例教程-javascript用createRange拷貝Range片段到粘貼板(代碼實例)全部内容,希望文章能够帮你解决js實例教程-javascript用createRange拷貝Range片段到粘貼板(代碼實例)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。