- 浏览: 331018 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (59)
- wicket (1)
- java (24)
- js (27)
- xml (3)
- Android (1)
- 服务器 (7)
- 数据库 (7)
- spring (1)
- hibernate (1)
- ssh (6)
- web (25)
- oracle (1)
- 函数 (2)
- mysql (1)
- 手机 (6)
- javascript (21)
- session (3)
- cookie (4)
- struts (1)
- ngnix (1)
- 软件使用 (2)
- linux (1)
- vi (1)
- 前端 (6)
- javascrip (2)
- html (3)
- js css (2)
- 正则 (1)
- ajax (1)
- toast (1)
- 消息提示 (1)
- 确认弹窗 (1)
- springmvc (1)
- 倒计时 (1)
最新评论
-
wenm168:
美观的图片上传前预览效果:http://www.anyrt.c ...
JavaScript 图片的上传前预览(兼容所有浏览器) -
口弦1992:
我的ie9并没有图片预览出来啊。
JavaScript 图片的上传前预览(兼容所有浏览器) -
axie1234567:
分享的东西很有用。
java web中实现同一帐号同一时间只能一个地点登陆(类似QQ登录的功能) -
wyl4728:
32个赞
mysql函数大全 -
yu_long:
seeker.getCountry(ip);
解析出来的 ...
根据IP地址获取所在地
最近在写一个复制相关代码到剪贴板的功能!
如果只是IE浏览器的话,可以利用window.clipboardData这个对象,相关js代码:
window.clipboardData.clearData(); var couponResult = window.clipboardData.setData("couponResult", couponResultHtml); if (couponResult) { alert("优惠券代码拷贝成功!"); } else { alert("优惠券代码拷贝失败!"); }
但这个对象是只针对IE的。。如果是其它的浏览器呢,则会用不了,这里提供两种相关功能的方法
1.
if (window.clipboardData) { window.clipboardData.clearData(); var couponResult = window.clipboardData.setData("couponResult", couponResultHtml); if (couponResult) { alert("优惠券代码拷贝成功!"); } else { alert("优惠券代码拷贝失败!"); } } else if (navigator.userAgent.indexOf("Opera") != -1) { window.location = couponResultHtml; } else if (window.netscape) { try { netscape.security.PrivilegeManager .enablePrivilege("UniversalXPConnect"); } catch (e) { alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'"); } var clip = Components.classes['@mozilla.org/widget/clipboard;1'] .createInstance(Components.interfaces.nsIClipboard); if (!clip) return; var trans = Components.classes['@mozilla.org/widget/transferable;1'] .createInstance(Components.interfaces.nsITransferable); if (!trans) return; trans.addDataFlavor('text/unicode'); var str = new Object(); var len = new Object(); var str = Components.classes["@mozilla.org/supports-string;1"] .createInstance(Components.interfaces.nsISupportsString); var copytext = couponResultHtml; str.data = copytext; trans.setTransferData("text/unicode", str, copytext.length * 2); var clipid = Components.interfaces.nsIClipboard; if (!clip) return false; clip.setData(trans, null, clipid.kGlobalClipboard); }
这个需要对浏览器进行设置,在浏览器栏中输入:about:config找到signed.applets.codebase_principal_supporty设置为true
2.得用flash的功能,现在网上有人写了一个ZeroClipboard.js,ZeroClipboard.swf
只要将这两文件作相关改动就行了:可以实现任意浏览器的复制,当然要浏览器支持flash插件才行!
ZeroClipboard.js相关代码:(此有经过改动),因为项目中有用到jquery,与其中的$操作符有功能上的冲突,所以改成了_(下划线),
// Simple Set Clipboard System // update 2011-04-20 // Author: Eric_wang var ZeroClipboard = { version: "1.0.6", clients: {}, // registered upload clients on page, indexed by id moviePath: rootStaticPath+'/js/activity/coupon/ZeroClipboard.swf', // URL to movie nextId: 1, // ID of next movie _: function(thingy) { // simple DOM lookup utility function if (typeof(thingy) == 'string') thingy = document.getElementById(thingy); if (!thingy.addClass) { // extend element with a few useful methods thingy.hide = function() { this.style.display = 'none'; }; thingy.show = function() { this.style.display = ''; }; thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; }; thingy.removeClass = function(name) { var classes = this.className.split(/\s+/); var idx = -1; for (var k = 0; k < classes.length; k++) { if (classes[k] == name) { idx = k; k = classes.length; } } if (idx > -1) { classes.splice( idx, 1 ); this.className = classes.join(' '); } return this; }; thingy.hasClass = function(name) { return !!this.className.match( new RegExp("\\s*" + name + "\\s*") ); }; } return thingy; }, setMoviePath: function(path) { // set path to ZeroClipboard.swf this.moviePath = path; }, dispatch: function(id, eventName, args) { // receive event from flash movie, send to client var client = this.clients[id]; if (client) { client.receiveEvent(eventName, args); } }, register: function(id, client) { // register new client to receive events this.clients[id] = client; }, getDOMObjectPosition: function(obj, stopObj) { // get absolute coordinates for dom element var info = { left: 0, top: 0, width: obj.width ? obj.width : obj.offsetWidth, height: obj.height ? obj.height : obj.offsetHeight }; while (obj && (obj != stopObj)) { info.left += obj.offsetLeft; info.top += obj.offsetTop; obj = obj.offsetParent; } return info; }, Client: function(elem) { // constructor for new simple upload client this.handlers = {}; // unique ID this.id = ZeroClipboard.nextId++; this.movieId = 'ZeroClipboardMovie_' + this.id; // register client with singleton to receive flash events ZeroClipboard.register(this.id, this); // create movie if (elem) this.glue(elem); } }; ZeroClipboard.Client.prototype = { id: 0, // unique ID for us ready: false, // whether movie is ready to receive events or not movie: null, // reference to movie object clipText: '', // text to copy to clipboard handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor cssEffects: true, // enable CSS mouse effects on dom container handlers: null, // user event handlers glue: function(elem, appendElem, stylesToAdd) { // glue to DOM element // elem can be ID or actual DOM element object this.domElement = ZeroClipboard._(elem); // float just above object, or zIndex 99 if dom element isn't set var zIndex = 99; if (this.domElement.style.zIndex) { zIndex = parseInt(this.domElement.style.zIndex, 10) + 1; } if (typeof(appendElem) == 'string') { appendElem = ZeroClipboard._(appendElem); } else if (typeof(appendElem) == 'undefined') { appendElem = document.getElementsByTagName('body')[0]; } // find X/Y position of domElement var box = ZeroClipboard.getDOMObjectPosition(this.domElement, appendElem); // create floating DIV above element this.div = document.createElement('div'); var style = this.div.style; style.position = 'absolute'; style.left = '' + box.left + 'px'; style.top = '' + box.top + 'px'; style.width = '' + box.width + 'px'; style.height = '' + box.height + 'px'; style.zIndex = zIndex; if (typeof(stylesToAdd) == 'object') { for (addedStyle in stylesToAdd) { style[addedStyle] = stylesToAdd[addedStyle]; } } // style.backgroundColor = '#f00'; // debug appendElem.appendChild(this.div); this.div.innerHTML = this.getHTML( box.width, box.height ); }, getHTML: function(width, height) { // return HTML for movie var html = ''; var flashvars = 'id=' + this.id + '&width=' + width + '&height=' + height; if (navigator.userAgent.match(/MSIE/)) { // IE gets an OBJECT tag var protocol = location.href.match(/^https/i) ? 'https://' : 'http://'; html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+protocol+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" 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="'+flashvars+'"/><param name="wmode" value="transparent"/></object>'; } else { // all other browsers get an EMBED tag html += '<embed id="'+this.movieId+'" src="'+ZeroClipboard.moviePath+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+this.movieId+'" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" wmode="transparent" />'; } return html; }, hide: function() { // temporarily hide floater offscreen if (this.div) { this.div.style.left = '-2000px'; } }, show: function() { // show ourselves after a call to hide() this.reposition(); }, destroy: function() { // destroy control and floater if (this.domElement && this.div) { this.hide(); this.div.innerHTML = ''; var body = document.getElementsByTagName('body')[0]; try { body.removeChild( this.div ); } catch(e) {;} this.domElement = null; this.div = null; } }, reposition: function(elem) { // reposition our floating div, optionally to new container // warning: container CANNOT change size, only position if (elem) { this.domElement = ZeroClipboard._(elem); if (!this.domElement) this.hide(); } if (this.domElement && this.div) { var box = ZeroClipboard.getDOMObjectPosition(this.domElement); var style = this.div.style; style.left = '' + box.left + 'px'; style.top = '' + box.top + 'px'; } }, setText: function(newText) { // set text to be copied to clipboard this.clipText = newText; if (this.ready) this.movie.setText(newText); }, addEventListener: function(eventName, func) { // add user event listener for event // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel eventName = eventName.toString().toLowerCase().replace(/^on/, ''); if (!this.handlers[eventName]) this.handlers[eventName] = []; this.handlers[eventName].push(func); }, setHandCursor: function(enabled) { // enable hand cursor (true), or default arrow cursor (false) this.handCursorEnabled = enabled; if (this.ready) this.movie.setHandCursor(enabled); }, setCSSEffects: function(enabled) { // enable or disable CSS effects on DOM container this.cssEffects = !!enabled; }, receiveEvent: function(eventName, args) { // receive event from flash eventName = eventName.toString().toLowerCase().replace(/^on/, ''); // special behavior for certain events switch (eventName) { case 'load': // movie claims it is ready, but in IE this isn't always the case... // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function this.movie = document.getElementById(this.movieId); if (!this.movie) { var self = this; setTimeout( function() { self.receiveEvent('load', null); }, 1 ); return; } // firefox on pc needs a "kick" in order to set these in certain cases if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) { var self = this; setTimeout( function() { self.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; } // switch eventName if (this.handlers[eventName]) { for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) { var func = this.handlers[eventName][idx]; if (typeof(func) == 'function') { // actual function reference func(this, args); } else if ((typeof(func) == 'object') && (func.length == 2)) { // PHP style object + method, i.e. [myObject, 'myMethod'] func[0][ func[1] ](this, args); } else if (typeof(func) == 'string') { // name of function window[func](this, args); } } // foreach event handler defined } // user defined handler for event } };
页面 相关
<div id="d_clip_button$j" class="my_clip_button" style="padding-left:30px;float:right;padding-top:5px;"><a href="javascript:void(0)" onclick="copyCoupon('$j')" style="color:#7E9AD2;font-weight:bold;">拷贝此优惠券代码进行推广</a></div>
页面调用JS
var clip = null; function _(id) { return document.getElementById(id); } function init(id) { //alert(""+id); clip = new ZeroClipboard.Client(); clip.setHandCursor(true); clip.addEventListener('load', function(){alert("flash加载完毕")}); clip.addEventListener('mouseDown', function(client){ var couponResultHtml = "<link rel=\"stylesheet\" href=\"" + rootStaticPath + "/css/shop/supplier/public.css\"/>"; var clipCouponHTML=$("#couponResult"+id).html(); //alert(couponResultHtml+clipCouponHTML); try{ clip.setText(couponResultHtml+clipCouponHTML); }catch(e){ alert("优惠券代码拷贝失败!"); } }); clip.addEventListener('complete', function(){alert("优惠券代码拷贝成功!");}
//flash容器ID
clip.glue( 'd_clip_button'+id );
}
function my_mouse_over(client) {
//将内容放入到剪贴板中
clip.setText( $("#couponResult0").html());
}
上传原文件如下:
大家如有什么问题可以邮件给我wangyong.ctit@gmail.com
- zeroclipboard.zip (5.8 KB)
- 下载次数: 94
发表评论
-
简单倒计时模板插件
2017-07-12 12:01 587Countdown.js (function() { ... -
简单上传文件插件
2017-07-12 11:46 614team777.upload.js js: ; ... -
简单数字键盘
2017-07-12 11:20 559好久没有写博客了,最近有个需求关于数字键盘的,找了下网上的 ... -
简化的原生ajax
2015-03-23 14:25 584/* * * path: seaAjax.js ... -
简单入门-JavaScript正则表达式
2015-03-23 14:19 805什么是正则表达式?这个问题可以参见:“正则表达式30分钟入门 ... -
Fxtpl v1.0 繁星前端模板引擎(非原创)
2014-08-18 13:48 3065Fxtpl 全新的JS模板引擎 Download t ... -
利用html5进行城市定位!aqi指数的获取
2014-07-08 17:52 21346<html><head> < ... -
seajs打包部署工具spm的使用总结
2014-06-30 14:22 898相信使用seajs的好处大家都是知道的,接触seajs好像 ... -
javaScript的性能优化
2014-01-17 14:05 1221随着网络的发展,网速 ... -
秒杀倒计时控件
2014-01-17 11:20 3118/** * User: yongwang * Da ... -
Nginx 下缓存静态文件(如css js)(转)
2013-05-31 13:48 13880目的:缓存nginx服务器的静态文件。如css,js, ... -
百度地图API文档
2012-11-13 18:31 1085sdfsfsdf -
JavaScript 图片的上传前预览(兼容所有浏览器)
2012-10-11 09:17 52453功能描述 通过 JavaScript 实现图片的 ... -
实现局部图片的懒加载
2012-08-31 14:52 5440此方式适用和iscroll结合使用,若用于普 ... -
iscroll实现图片的循环滑动
2012-08-31 10:19 2191<div class="gexian" ... -
iScroll框架的修改
2012-06-29 16:05 1771最近做的一个项目,用到了iScroll框架,在使用过程中 ... -
客户端元素滑动和滚动效果实现
2012-06-29 15:55 2703最近一直在搞一些有关元素动态展示的功能,这么做是为了增加 ... -
iScroll 左右滑动的时候,阻止上下滑动e.preventDefault()
2012-06-29 15:54 11759scroll.js 触屏手机 滚动效果代码库! 很不错 ... -
iscroll介绍
2012-06-12 18:52 0参考 http://hi.baidu.com/xiaowuph ... -
JS页面跳转和js对iframe进行页面跳转、刷新
2012-04-17 10:48 3932一、JS方式的页面跳转1.window.loc ...
相关推荐
通过flash实现的复制代码到剪切板,兼容多种浏览器ie,firefox,opera,chorme
实现思路: 1、在页面加载完成之后,使用js给每个pre标签增加一个按钮“复制代码” 2、给按钮增加点击事件,点击事件的功能就是复制代码块的内容 实现代码: css部分,btn-pre-copy是pre标签中使用js增加的...
两天在做Web前端时,遇到需求通过js实现文本复制的功能。经过一番测试,终于实现了出来,有需要的小伙伴可以参考下。
超炫切换效果焦点图,兼容各种主流浏览器 使用方法: 1、依次引入slideshow.css与...2、复制代码到页面中即可。 3、因效果过于复杂,导致slideshow.js文件达到了255KB的大小,可使用js压缩工具压缩至84KB左右。
2、将本目录"代码,记得修改!.php"代码全部复制到网站根目录下index.php的开头。 3、复制前请先修改本站名字、链接 替换成 自己的网站名字、链接。 批量替换(可用notepad++等软件进行批量修改): 使用说明: ...
山东大学选课脚本 使用方法 将js文件中的代码复制粘贴至浏览器控制台中,按下回车自动执行代码。 终止方法:退出当前页面或按下f5刷新页面。
因为网站文章需要提供几个按钮,单击后实现复制文章内容到剪贴板。 在网上搜索了很多内容,发现都比较乱这里自己整理下,分享给大家 效果图如下: 之前使用的是window.clipboardData.setData,只能支持IE和火狐。...
Zero Clipboard实现简单跨浏览器复制 var clip = new ZeroClipboard.Client(); // 新建一个对象 clip.setHandCursor( true ); // 设置鼠标为手型 clip.setText("哈哈"); // 设置要复制的文本。 // 注册一个 button,...
CodeCopy 浏览器扩展实现在每个代码块上添加一个复制至剪贴板的按钮
这两天在做Web前端时,遇到需求通过js实现文本复制的功能。 先不考虑浏览器的兼容性,看看各浏览器对复制功能的支持情况: 1、IE浏览器,解决方法有三种,代码如下: 代码如下: function copy(txtid){ var txtObj = ...
IE中,浏览器显示窗口大小只能以下获取: 代码如下复制代码 代码如下:document.body.offsetWidth document.body.offsetHeight在声明了DOCTYPE的浏览器中,可以用以下来获取浏览器显示窗口大小: 代码如下复制代码 ...
一键复制微信号,适用web端,移动端,微信端,亲测有效,直接下载直接上手,很方便使用,分享给大家。移动端复制微信代码-跳转微信代码-微信复制代码-点击复制微信号。
pasteimg是一款可以在浏览器中实现图片粘贴的jQuery插件,兼容Chrome、Firefox、IE11以及其他使用这些内核的浏览器,比如,国内著名的360浏览器。 pasteimg可以识别浏览器中直接复制的图片,也可以识别复制的富...
内容功能特性 使用原生 JavaScript 开发,可在浏览器中运行 支持 JavaScript 代码的高亮,折叠,复制,删除,撤销,重做等操作 支持 JavaScript 代码的常见语法错误检查(开发中,有兴趣的朋友可Pull Request) 支持...
yxy.js 为普通的右下角广告,直接调用活复制代码到您的js文件里面即可。 guanbi-no.js 右下角广告代码,增加了两个地址与图片随机变换。 yxy_close.js 右下角广告代码,比上个版本增加了关闭功能,更符合用户...
目前,如果使用javascript来写复制到剪贴板的代码,一般都是浏览器不兼容的。所以采用flash的方式,模拟一个层,再来复制,就可以做到全部浏览器都适用哦~ 需要下载一个swf文件,和一个js文件。把这两个文件,和htm...
jquery+flash实现点击复制到剪切板。 1. 加载jQuery和zClip,地址请根据各自的存放地址做相应修改 2.js实现点击复制的代码(注:请上传到服务器上测试)
clipboard.js是一个github上的开源项目,clipboard.js 实现了纯 JavaScript (无 Flash)的浏览器内容复制到系统剪贴板的功能。可以在浏览器和 Node 环境中使用。支持 Chrome 42+、Firefox 41+、IE 9+、Opera 29+