JavaScript實現(xiàn)復(fù)制或剪切內(nèi)容到剪貼板功能的方法
項目中需要實現(xiàn)一個點擊按鈕復(fù)制鏈接的功能,網(wǎng)上看到的幾款插件,ZeroClipboard是通過flash實現(xiàn)的復(fù)制功能,隨著越來越多的提議廢除flash,能不能通過JS來實現(xiàn)復(fù)制剪切呢,今天分享一個兼容IE7瀏覽器復(fù)制的插件給大家,支持使用javascript實現(xiàn)復(fù)制、剪切和粘貼。
方法。
復(fù)制
var copy = new clipBoard(document.getElementById('data'), { beforeCopy: function() { }, copy: function() { return document.getElementById('data').value; }, afterCopy: function() { } });
復(fù)制將自動被調(diào)用,如果你想要自己調(diào)用:
var copy = new clipBoard(document.getElementById('data')); copy.copyd();
document.getElementById('data') :要獲取的對象, 你也可以使用jQuery $('#data')
剪切
基本上與復(fù)制的實現(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(); } };
相關(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實現(xiàn)全屏滾動效果
這篇文章主要為大家詳細(xì)介紹了fullPage.js和CSS3實現(xiàn)全屏滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05基于HTML5上使用iScroll實現(xiàn)下拉刷新,上拉加載更多
本文主要介紹在HTML5中使用iScroll實現(xiàn)下拉刷新,上拉加載更多數(shù)據(jù)的方法,主要就是寫了兩個自定義函數(shù)pullDownAction和pullUpAction,分別在下拉和上拉的事件中調(diào)用他們。2016-05-05JS中style.display和style.visibility的區(qū)別實例說明
下面的例子說明了這種區(qū)別:在這個例子中,divContent1和divContent2隱藏的時候用的是style.display=none,這時候,后面的div會向上移動,占據(jù)已經(jīng)隱藏的div的空間。divContent3和divContent4用的是style.visibility=hidden來隱藏,但是其隱藏后仍然占據(jù)原來的空間2013-03-03