最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Javascript復(fù)制實(shí)例詳解

 更新時(shí)間:2016年01月28日 14:05:42   作者:chenpingzhao  
最近接了個(gè)項(xiàng)目,其中有項(xiàng)目需求是這樣的,需要復(fù)制內(nèi)容到剪切板,因?yàn)橛斜姸酁g覽器,所以兼容性很重要,下面通過本文給大家介紹Javascript復(fù)制實(shí)例詳解,對(duì)js復(fù)制相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧

在做項(xiàng)目時(shí)有一個(gè)需求,是需要復(fù)制內(nèi)容到剪切板,因?yàn)橛斜姸酁g覽器,所以要兼容性很重要。

1、最簡(jiǎn)單的copy,只能在IE下使用

使用clipboardData方法

<script type="text/javascript">
function copy(){
window.clipboardData.setData("text",document.getElementById("name").value);
alert("The text is on the clipboard, try to paste it!");
}
</script> 

2、跨瀏覽器的,但是Firefox無法復(fù)制

<head>
<script type="text/javascript">
function CopyToClipboard () {
var input = document.getElementById ("toClipboard");
var textToClipboard = input.value;
var success = true;
if (window.clipboardData) { // Internet Explorer
window.clipboardData.setData ("Text", textToClipboard);
}
else {
// create a temporary element for the execCommand method
var forExecElement = CreateElementForExecCommand (textToClipboard);
/* Select the contents of the element 
(the execCommand for 'copy' method works on the selection) */
SelectContent (forExecElement);
var supported = true;
// UniversalXPConnect privilege is required for clipboard access in Firefox
try {
if (window.netscape && netscape.security) {
netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
}
// Copy the selected content to the clipboard
// Works in Firefox and in Safari before version 5
success = document.execCommand ("copy", false, null);
}
catch (e) {
success = false;
}
// remove the temporary element
document.body.removeChild (forExecElement);
}
if (success) {
alert ("The text is on the clipboard, try to paste it!");
}
else {
alert ("Your browser doesn't allow clipboard access!");
}
}
function CreateElementForExecCommand (textToClipboard) {
var forExecElement = document.createElement ("div");
// place outside the visible area
forExecElement.style.position = "absolute";
forExecElement.style.left = "-10000px";
forExecElement.style.top = "-10000px";
// write the necessary text into the element and append to the document
forExecElement.textContent = textToClipboard;
document.body.appendChild (forExecElement);
// the contentEditable mode is necessary for the execCommand method in Firefox
forExecElement.contentEditable = true;
return forExecElement;
}
function SelectContent (element) {
// first create a range
var rangeToSelect = document.createRange ();
rangeToSelect.selectNodeContents (element);
// select the contents
var selection = window.getSelection ();
selection.removeAllRanges ();
selection.addRange (rangeToSelect);
}
</script>
</head>
<body>
<input id="toClipboard" value="text to clipboard"/>
<button onclick='CopyToClipboard ()'>Copy text to clipboard</button>
</body>

測(cè)試后,F(xiàn)irefox訪問失敗

3、萬能的flash

不要重復(fù)造輪子了,有一個(gè)使用廣泛的類庫(kù)ZeroClipboard

Zero Clipboard 的實(shí)現(xiàn)原理

Zero Clipboard 利用 Flash 進(jìn)行復(fù)制,之前有 Clipboard Copy 解決方案,其利用的是一個(gè)隱藏的 Flash。但最新的 Flash Player 10 只允許在 Flash 上進(jìn)行操作才能啟動(dòng)剪貼板。所以 Zero Clipboard 對(duì)此進(jìn)行了改進(jìn),用了一個(gè)透明的 Flash ,讓其漂浮在按鈕之上,這樣其實(shí)點(diǎn)擊的不是按鈕而是 Flash ,也就可以使用 Flash 的復(fù)制功能了。

創(chuàng)建一個(gè)透明的flash

將這個(gè)flash浮在按鈕上層

確定要復(fù)制的文本是什么

監(jiān)聽這個(gè)透明flash的鼠標(biāo)點(diǎn)擊事件

該flash被點(diǎn)擊之后,完成剪切板處理

對(duì)于這幾件事,ZeroClipboard分別提供了不同的api,來完成整個(gè)需求

如何使用 Zero Clipboard

git clone https://github.com/chenpingzhao/easycopy.git

關(guān)于ZeroClipboard.js

var ZeroClipboard = {
version: "1.0.7",
clients: {},
moviePath: "zeroclipboard.swf",
nextId: 1,
$: function(A) {
if (typeof(A) == "string") {
A = document.getElementById(A)
}
if (!A.addClass) {
A.hide = function() {
this.style.display = "none"
};
A.show = function() {
this.style.display = ""
};
A.addClass = function(B) {
this.removeClass(B);
this.className += " " + B
};
A.removeClass = function(D) {
var E = this.className.split(/\s+/);
var B = -1;
for (var C = 0; C < E.length; C++) {
if (E[C] == D) {
B = C;
C = E.length
}
}
if (B > -1) {
E.splice(B, 1);
this.className = E.join(" ")
}
return this
};
A.hasClass = function(B) {
return !!this.className.match(new RegExp("\\s*" + B + "\\s*"))
}
}
return A
},
setMoviePath: function(A) {
this.moviePath = A
},
dispatch: function(D, B, C) {
var A = this.clients[D];
if (A) {
A.receiveEvent(B, C)
}
},
register: function(B, A) {
this.clients[B] = A
},
getDOMObjectPosition: function(C, A) {
var B = {
left: 0,
top: 0,
width: C.width ? C.width : C.offsetWidth,
height: C.height ? C.height : C.offsetHeight
};
while (C && (C != A)) {
B.left += C.offsetLeft;
B.top += C.offsetTop;
C = C.offsetParent
}
return B
},
Client: function(A) {
this.handlers = {};
this.id = ZeroClipboard.nextId++;
this.movieId = "ZeroClipboardMovie_" + this.id;
ZeroClipboard.register(this.id, this);
if (A) {
this.glue(A)
}
}
};
ZeroClipboard.Client.prototype = {
id: 0,
ready: false,
movie: null,
clipText: "",
handCursorEnabled: true,
cssEffects: true,
handlers: null,
//我們可以通過下面這個(gè)api,將flash和按鈕重疊,且浮在按鈕之上
glue: function(D, B, E) {
this.domElement = ZeroClipboard.$(D);
var F = 99;
if (this.domElement.style.zIndex) {
F = parseInt(this.domElement.style.zIndex, 10) + 1
}
if (typeof(B) == "string") {
B = ZeroClipboard.$(B)
} else {
if (typeof(B) == "undefined") {
B = document.getElementsByTagName("body")[0]
}
}
var C = ZeroClipboard.getDOMObjectPosition(this.domElement, B);
this.div = document.createElement("div");
var A = this.div.style;
A.position = "absolute";
A.left = "" + C.left + "px";
A.top = "" + C.top + "px";
A.width = "" + C.width + "px";
A.height = "" + C.height + "px";
A.zIndex = F;
if (typeof(E) == "object") {
for (addedStyle in E) {
A[addedStyle] = E[addedStyle]
}
}
B.appendChild(this.div);
this.div.innerHTML = this.getHTML(C.width, C.height)
},
/*IE 的 Flash JavaScript 通信接口上有一個(gè) bug 。
你必須插入一個(gè) object 標(biāo)簽到一個(gè)已存在的 DOM 元素中。并且在寫入 innerHTML 之前請(qǐng)確保該元素已經(jīng) appendChild 方法插入到 DOM 中*/
getHTML: function(D, A) {
var C = "";
var B = "id=" + this.id + "&width=" + D + "&height=" + A;
if (navigator.userAgent.match(/MSIE/)) {
var E = location.href.match(/^https/i) ? "https://" : "http://";
C += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="' + E + 'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + D + '" height="' + A + '" id="' + this.movieId + '" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="' + ZeroClipboard.moviePath + '" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="' + B + '"/><param name="wmode" value="transparent"/></object>'
} else {
C += '<embed id="' + this.movieId + '" src="' + ZeroClipboard.moviePath + '" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="' + D + '" height="' + A + '" name="' + this.movieId + '" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + B + '" wmode="transparent" />'
}
return C
},
hide: function() {
if (this.div) {
this.div.style.left = "-2000px"
}
},
show: function() {
this.reposition()
},
destroy: function() {
if (this.domElement && this.div) {
this.hide();
this.div.innerHTML = "";
var A = document.getElementsByTagName("body")[0];
try {
A.removeChild(this.div)
} catch (B) {}
this.domElement = null;
this.div = null
}
},
/* 因?yàn)榘粹o上漂浮有一個(gè) Flash 按鈕,所以當(dāng)頁面大小發(fā)生變化時(shí),F(xiàn)lash 按鈕可能會(huì)錯(cuò)位,就點(diǎn)不著了
Zero Clipboard 提供了一個(gè) reposition() 方法,可以重新計(jì)算 Flash 按鈕的位置。我們可以將它綁定到 resize 事件上
bind(window, "resize", function(){ clip.reposition(); }); 
function bind(obj, type, fn) {
if (obj.attachEvent) {
obj['e' + type + fn] = fn;
obj[type + fn] = function() {
obj['e' + type + fn](window.event);
}
obj.attachEvent('on' + type, obj[type + fn]);
} else
obj.addEventListener(type, fn, false);
}*/
reposition: function(C) {
if (C) {
this.domElement = ZeroClipboard.$(C);
if (!this.domElement) {
this.hide()
}
}
if (this.domElement && this.div) {
var B = ZeroClipboard.getDOMObjectPosition(this.domElement);
var A = this.div.style;
A.left = "" + B.left + "px";
A.top = "" + B.top + "px"
}
},
setText: function(A) {
this.clipText = A;
if (this.ready) {
this.movie.setText(A)
}
},
addEventListener: function(A, B) {
A = A.toString().toLowerCase().replace(/^on/, "");
if (!this.handlers[A]) {
this.handlers[A] = []
}
this.handlers[A].push(B)
},
setHandCursor: function(A) {
this.handCursorEnabled = A;
if (this.ready) {
this.movie.setHandCursor(A)
}
},
/*鼠標(biāo)移到按鈕上或點(diǎn)擊時(shí),由于有 Flash 按鈕的遮擋,所以像 css ":hover", ":active" 等偽類可能會(huì)失效。
setCSSEffects() 方法就是解決這個(gè)問題。首先我們需要將偽類改成類
copy - botton: hover {
border - color: #FF6633;
}

可以改成下面的 ":hover" 改成 ".hover"

copy - botton.hover {
border - color: #FF6633;
}

我們可以調(diào)用 clip.setCSSEffects( true ); 這樣 Zero Clipboard 會(huì)自動(dòng)為我們處理:將類 .hover 當(dāng)成偽類 :hover*/

setCSSEffects: function(A) {
this.cssEffects = !! A
},
/*Zero Clipboard 提供了一些事件,你可以自定義函數(shù)處理這些事件。
Zero Clipboard 事件處理函數(shù)為 addEventListener(); 例如當(dāng) Flash 完全載入后會(huì)觸發(fā)一個(gè)事件 "load" 
clip.addEventListener( "load", function(client) { 
alert("Flash 加載完畢!"); 
});*/
receiveEvent: function(D, E) {
D = D.toString().toLowerCase().replace(/^on/, "");
switch (D) {
case "load":
this.movie = document.getElementById(this.movieId);
if (!this.movie) {
var C = this;
setTimeout(function() {
C.receiveEvent("load", null)
}, 1);
return
}
if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
var C = this;
setTimeout(function() {
C.receiveEvent("load", null)
}, 100);
this.ready = true;
return
}
this.ready = true;
this.movie.setText(this.clipText);
this.movie.setHandCursor(this.handCursorEnabled);
break;
case "mouseover":
if (this.domElement && this.cssEffects) {
this.domElement.addClass("hover");
if (this.recoverActive) {
this.domElement.addClass("active")
}
}
break;
case "mouseout":
if (this.domElement && this.cssEffects) {
this.recoverActive = false;
if (this.domElement.hasClass("active")) {
this.domElement.removeClass("active");
this.recoverActive = true
}
this.domElement.removeClass("hover")
}
break;
case "mousedown":
if (this.domElement && this.cssEffects) {
this.domElement.addClass("active")
}
break;
case "mouseup":
if (this.domElement && this.cssEffects) {
this.domElement.removeClass("active");
this.recoverActive = false
}
break
}
if (this.handlers[D]) {
for (var B = 0, A = this.handlers[D].length; B < A; B++) {
var F = this.handlers[D][B];
if (typeof(F) == "function") {
F(this, E)
} else {
if ((typeof(F) == "object") && (F.length == 2)) {
F[0][F[1]](this, E)
} else {
if (typeof(F) == "string") {
window[F](this, E)
}
}
}
}
}
}
}; 

以上所述是小編給大家介紹的Javascript復(fù)制實(shí)例詳解,希望對(duì)大家有所幫助。

相關(guān)文章

  • 完美兼容IE,chrome,ff的設(shè)為首頁、加入收藏及保存到桌面js代碼

    完美兼容IE,chrome,ff的設(shè)為首頁、加入收藏及保存到桌面js代碼

    這篇文章主要給大家分享了完美兼容IE,chrome,ff的設(shè)為首頁、收藏本站及保存到桌面js代碼,有需要的小伙伴參考下。
    2014-12-12
  • 使用JS判斷移動(dòng)端手機(jī)橫豎屏狀態(tài)

    使用JS判斷移動(dòng)端手機(jī)橫豎屏狀態(tài)

    本文通過js和cas代碼分別給大家介紹了移動(dòng)端判斷手機(jī)橫豎屏狀態(tài)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • 詳解Javascript中document.execCommand()的用法以及指令參數(shù)列表

    詳解Javascript中document.execCommand()的用法以及指令參數(shù)列表

    execCommand方法是執(zhí)行一個(gè)對(duì)當(dāng)前文檔,當(dāng)前選擇或者給出范圍的命令。在HTML5中,execCommand可以通過JavaScript代碼來調(diào)用,使得開發(fā)者可以在網(wǎng)頁中實(shí)現(xiàn)一些復(fù)雜的文本操作。在HTML編輯器中這個(gè)命令用得很多,酷炫的強(qiáng)大功能。
    2023-07-07
  • js中判斷一個(gè)數(shù)是不是素?cái)?shù)的三種方法例子

    js中判斷一個(gè)數(shù)是不是素?cái)?shù)的三種方法例子

    這篇文章主要給大家介紹了關(guān)于js中如何判斷一個(gè)數(shù)是不是素?cái)?shù)的三種方法,素?cái)?shù)(只能被1和本身整除的數(shù))規(guī)律:把這個(gè)數(shù)除以它之前的每一個(gè)數(shù)(從2開始)只要找到一個(gè)整除(余數(shù)為0)就是非素?cái)?shù),需要的朋友可以參考下
    2023-10-10
  • 點(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)的簡(jiǎn)單js代碼(推薦)

    點(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)的簡(jiǎn)單js代碼(推薦)

    下面小編就為大家?guī)硪黄c(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)的簡(jiǎn)單js代碼(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • js數(shù)組刪除問題(splice和delete的用法)

    js數(shù)組刪除問題(splice和delete的用法)

    這篇文章主要介紹了js數(shù)組刪除問題(splice和delete的用法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • JS 文件傳參及處理技巧分析

    JS 文件傳參及處理技巧分析

    其實(shí)為js文件傳參是很久就接觸過的一個(gè)問題,只是一直沒有放在心上,今天在無憂看到又有人問這個(gè)問題,今日總結(jié)一下。
    2010-05-05
  • 基于函數(shù)執(zhí)行的踩坑(addEventListener)

    基于函數(shù)執(zhí)行的踩坑(addEventListener)

    這篇文章主要介紹了基于函數(shù)執(zhí)行的踩坑(addEventListener),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • JS字符串常用操作方法實(shí)例小結(jié)

    JS字符串常用操作方法實(shí)例小結(jié)

    這篇文章主要介紹了JS字符串常用操作方法,結(jié)合實(shí)例形式總結(jié)分析了javascript字符串獲取、返回、拼接、替換、搜索、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下
    2019-06-06
  • js中.sort()函數(shù)的常見用法與高級(jí)操作

    js中.sort()函數(shù)的常見用法與高級(jí)操作

    JavaScript中的sort函數(shù)可以用來對(duì)數(shù)組進(jìn)行排序,默認(rèn)情況下sort函數(shù)將數(shù)組中的元素轉(zhuǎn)換為字符串,并按照Unicode碼點(diǎn)的順序進(jìn)行排序,下面這篇文章主要給大家介紹了關(guān)于js中.sort()函數(shù)的常見用法與高級(jí)操作的相關(guān)資料,需要的朋友可以參考下
    2023-05-05

最新評(píng)論

平利县| 永昌县| 盘山县| 溧阳市| 阜南县| 潮安县| 思南县| 石柱| 泰兴市| 德清县| 曲周县| 焦作市| 凤山市| 乐东| 西乌珠穆沁旗| 普定县| 普兰店市| 来凤县| 南通市| 襄垣县| 阆中市| 洛扎县| 汉中市| 永福县| 米脂县| 来凤县| 通山县| 大足县| 邓州市| 新巴尔虎左旗| 定安县| 那坡县| 延津县| 瓦房店市| 潜山县| 紫云| 长寿区| 读书| 济源市| 南通市| 海口市|