JavaScript實(shí)現(xiàn)復(fù)制或剪切內(nèi)容到剪貼板功能的方法
項(xiàng)目中需要實(shí)現(xiàn)一個(gè)點(diǎn)擊按鈕復(fù)制鏈接的功能,網(wǎng)上看到的幾款插件,ZeroClipboard是通過flash實(shí)現(xiàn)的復(fù)制功能,隨著越來越多的提議廢除flash,能不能通過JS來實(shí)現(xiàn)復(fù)制剪切呢,今天分享一個(gè)兼容IE7瀏覽器復(fù)制的插件給大家,支持使用javascript實(shí)現(xiàn)復(fù)制、剪切和粘貼。
方法。
復(fù)制
var copy = new clipBoard(document.getElementById('data'), { beforeCopy: function() { }, copy: function() { return document.getElementById('data').value; }, afterCopy: function() { } });
復(fù)制將自動(dòng)被調(diào)用,如果你想要自己調(diào)用:
var copy = new clipBoard(document.getElementById('data')); copy.copyd();
document.getElementById('data') :要獲取的對象, 你也可以使用jQuery $('#data')
剪切
基本上與復(fù)制的實(shí)現(xiàn)方法相同:
var cut = new clipBoard(document.getElementById('data'), { beforeCut: function() { }, cut: function() { return document.getElementById('data').value; }, afterCut: function() { } });
或者
var cut = new clipBoard(document.getElementById('data')); cut.cut(); paste var paste = new clipBoard(document.getElementById('data'), { beforePaste: function() { }, paste: function() { return document.getElementById('data').value; }, afterPaste: function() { } });
或者
var paste = new clipBoard(document.getElementById('data')); paste.paste();
完整代碼:
(function(name, fun) { if (typeof module !== 'undefined' && module.exports) { module.exports = fun(); } else if (typeof define === 'function' && define.amd) { define(fun); } else { this[name] = fun(); } })('clipBoard', function() { "use strict"; function clipBoard(tar, options) { this.options = options || {}; this.tar = tar[0] || tar; // if options contain copy, copy will be applied soon if (this.options.copy) { this.copyd(); } if(this.options.cut) { this.cut(); } if(this.options.paste) { this.paste(); } } clipBoard.prototype.copyd = function(value) { // before the copy it will be called, you can check the value or modify the value if (this.options.beforeCopy) { this.options.beforeCopy(); } // if the options set copy function, the value will be set. then get the paramer value. // above all, if the value is null, then will be set the tar of value value = value || this.tar.value || this.tar.innerText; if (this.options.copy) { value = this.options.copy(); } // for modern browser if (document.execCommand) { var element = document.createElement('SPAN'); element.textContent = value; document.body.appendChild(element); if (document.selection) { var range = document.body.createTextRange(); range.moveToElementText(element); range.select(); } else if (window.getSelection) { var range = document.createRange(); range.selectNode(element); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); } document.execCommand('copy'); element.remove ? element.remove() : element.removeNode(true); } // for ie if (window.clipboardData) { window.clipboardData.setData('text', value); } // after copy if (this.options.afterCopy) { this.options.afterCopy(); } };
- js實(shí)現(xiàn)各種復(fù)制到剪貼板的方法(分享)
- JavaScript復(fù)制內(nèi)容到剪貼板的兩種常用方法
- vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板的3種方案
- JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能兼容所有瀏覽器(推薦)
- js復(fù)制內(nèi)容到剪貼板代碼,js復(fù)制代碼的簡單實(shí)例
- 一段多瀏覽器的"復(fù)制到剪貼板"javascript代碼
- JS復(fù)制到剪貼板示例代碼
- JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能
- JavaScript實(shí)現(xiàn)頁面點(diǎn)擊復(fù)制到剪粘版并解決報(bào)錯(cuò)問題
相關(guān)文章
js中獲取鍵盤按下鍵值event.keyCode、event.charCode和event.which的兼容性詳解
這篇文章主要給大家介紹了關(guān)于Javascript中獲取鍵盤按下鍵值event.keyCode、event.charCode和event.which的兼容性的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03fullPage.js和CSS3實(shí)現(xiàn)全屏滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了fullPage.js和CSS3實(shí)現(xiàn)全屏滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05基于HTML5上使用iScroll實(shí)現(xiàn)下拉刷新,上拉加載更多
本文主要介紹在HTML5中使用iScroll實(shí)現(xiàn)下拉刷新,上拉加載更多數(shù)據(jù)的方法,主要就是寫了兩個(gè)自定義函數(shù)pullDownAction和pullUpAction,分別在下拉和上拉的事件中調(diào)用他們。2016-05-05JS中style.display和style.visibility的區(qū)別實(shí)例說明
下面的例子說明了這種區(qū)別:在這個(gè)例子中,divContent1和divContent2隱藏的時(shí)候用的是style.display=none,這時(shí)候,后面的div會向上移動(dòng),占據(jù)已經(jīng)隱藏的div的空間。divContent3和divContent4用的是style.visibility=hidden來隱藏,但是其隱藏后仍然占據(jù)原來的空間2013-03-03echarts實(shí)現(xiàn)排名柱狀圖的示例代碼
在ECharts中,可以通過設(shè)置數(shù)據(jù)的順序來控制柱狀圖的排序,本文就介紹了echarts實(shí)現(xiàn)排名柱狀圖的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09