<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>断桥残雪部落格ZeroClipboard</title>
	<atom:link href="http://www.js8.in/tag/zeroclipboard/feed" rel="self" type="application/rss+xml" />
	<link>http://www.js8.in</link>
	<description>专注前端开发，分享JS、jQuery、PHP、CSS、WEB开发技巧</description>
	<lastBuildDate>Wed, 08 Sep 2010 09:58:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<ttl>30</ttl>
		<item>
		<title>使用ZeroClipboard解决跨浏览器复制到剪贴板的问题</title>
		<link>http://www.js8.in/407.html</link>
		<comments>http://www.js8.in/407.html#comments</comments>
		<pubDate>Thu, 24 Dec 2009 09:48:06 +0000</pubDate>
		<dc:creator>断桥残雪</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[网络技术]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ZeroClipboard]]></category>

		<guid isPermaLink="false">http://www.js8.in/?p=407</guid>
		<description><![CDATA[其实开发中经常会用到复制的功能，并且会遇见在Firefox，Chrome，Opera，Safari能复制链接的兼容性问题，在 IE 下通过JavaScript的window.clipboardData.setData可以简单实现复制到剪贴板功能。但要想做到跨浏览器比较困难了。最后终于找到了终极解决方式：使用jhuckaby写的Zero Clipboard 。它利用 Flash 进行复制，所以只要浏览器装有Flash 就可以运行]]></description>
			<content:encoded><![CDATA[<h3>导言</h3>
<p>之前在开发<a target="_blank" href="http://www.pujia.com" target="_blank">普加</a>09年国庆中秋祝福页面复制链接功能时，遇见在Firefox，Chrome，Opera，Safari能复制链接的<strong>兼容性</strong>问题，而在近期上线的<a target="_blank" href="http://ditu.pujia.com/preview"  target="_blank">普加地图</a>产品中使用的是Flash的<strong>flashvars</strong>来传入参数来解决了这个问题。其实开发中经常会用到复制的功能，在 IE 下通过JavaScript的<strong>window.clipboardData.setData</strong>可以简单实现复制到剪贴板功能。最后终于找到了终极解决方式：使用jhuckaby写的Zero Clipboard的js类库。此方法利用 Flash完成进行复制内容到剪贴板，所以只要浏览器装有Flash 就可以复制内容。</p>
<h3>Zero Clipboard的实现原理</h3>
<p><strong>Zero Clipboard </strong>利用透明的Flash让其漂浮在复制按钮之上，这样其实点击的不是按钮而是 Flash ，这样将需要的内容传入Flash，再通过Flash的复制功能把传入的内容复制到<strong>剪贴板</strong>。</p>
<h3>Zero Clipboard的安装方法</h3>
<p>其实也不算安装啦，就是使用前的准备工作。<br />
首先需要下载 Zero Clipboard的压缩包，解压后把文件夹中两个文件：ZeroClipboard.js 和 ZeroClipboard.swf 放入到你的项目中。</p>
<blockquote><p>Zero Clipboard : [<a target="_blank" href="http://code.google.com/p/zeroclipboard/" target="_blank">项目主页</a>] [<a target="_blank" href="http://code.google.com/p/zeroclipboard/downloads/list" target="_blank">zip下载</a>] [<a target="_blank" href="http://bowser.macminicolo.net/~jhuckaby/zeroclipboard/" target="_blank">在线演示</a>]</p></blockquote>
<p><span id="more-407"></span><br />
然后把在你要使用复制功能的页面中引入Zero Clipboard的js文件：ZeroClipboard.js<br />
如下代码：</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;ZeroClipboard.js&quot;&gt;&lt;/script&gt;</pre></div></div>

</blockquote>
<p>注意：以上 ZeroClipboard.js, ZeroClipboard.swf需要放在同一路径下。如果不在同一路径，可使用ZeroClipboard.setMoviePath( &#8220;Flash路径&#8221; );来设置ZeroClipboard.swf 地址</p>
<h3>Zero Clipboard实现简单<strong>跨浏览器</strong>复制</h3>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> clip <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ZeroClipboard.<span style="color: #660066;">Client</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 新建一个对象</span>
clip.<span style="color: #660066;">setHandCursor</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 设置鼠标为手型</span>
clip.<span style="color: #660066;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;哈哈&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 设置要复制的文本。</span>
<span style="color: #006600; font-style: italic;">// 注册一个 button，参数为 id。点击这个 button 就会复制。</span>
<span style="color: #006600; font-style: italic;">//这个 button 不一定要求是一个 input 按钮，也可以是其他 DOM 元素。</span>
clip.<span style="color: #660066;">glue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;copy-botton&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 和上一句位置不可调换</span></pre></div></div>

</blockquote>
<p>这样，这样基本功能实现了，点击按钮就可以复制设置好的文本了。你可能注意到了，待复制的文本是固定的，如果想要动态改变的怎么办，比如复制一个输入框中的内容。不用担心，下面会讲到的。</p>
<h4>Zero Clipboard的高级功能</h4>
<p><strong>1、reposition() 方法</strong></p>
<p>因为按钮上漂浮有一个 Flash 按钮，所以当页面大小发生变化时，Flash 按钮可能会错位，这样就点不着了。 不要紧，Zero Clipboard 提供了一个 reposition() 方法，可以重新计算 Flash 按钮的位置。我们可以将它绑定到 resize 事件上。如下面代码是在jQuery下实现的resize事件重新设置按钮位置：<br />
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    clip.<span style="color: #660066;">reposition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</blockquote>
<p><strong>2、hide() 和 show() 方法</strong></p>
<p>这两个方法可以隐藏和显示 Flash 按钮 。其中 show() 方法会调用 reposition() 方法。</p>
<p><strong>3、setCSSEffects() 方法</strong></p>
<p>当鼠标移到按钮上或点击时，由于有 Flash 按钮的遮挡，所以像 css &#8220;:hover&#8221;, &#8220;:active&#8221; 等伪类可能会失效。setCSSEffects() 方法就是解决这个问题。首先我们需要将伪类改成类，比如：</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#copy-botton</span><span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">border-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#FF6633</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
// 可以改成下面的 <span style="color: #ff0000;">&quot;:hover&quot;</span> 改成 <span style="color: #ff0000;">&quot;.hover&quot;</span>
<span style="color: #cc00cc;">#copy-botton</span>.hover<span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">border-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#FF6633</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

</blockquote>
<p>我们可以调用 clip.setCSSEffects( true ); 这样 Zero Clipboard 会自动为我们处理：将类 .hover 当成伪类 :hover 。</p>
<p><strong>4、getHTML() 方法</strong></p>
<p>如果你想自己实例一个 Flash ，不用 Zero Clipboard 的附着方法，那么这个方法就可以帮上忙了。它接受两个参数，分别为 Flash 的宽度和高度。返回的是 Flash 对应的 HTML 代码。例如：</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> clip.<span style="color: #660066;">getHTML</span><span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</blockquote>
<p>你可以用 innerHTML 或直接 document.write(); 进行输出。<br />
以下是测试输出的组装完毕的HTML 代码：</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;embed id=&quot;ZeroClipboardMovie_1&quot; src=&quot;zeroclipboard/ZeroClipboard.swf&quot; loop=&quot;false&quot; menu=&quot;false&quot; quality=&quot;best&quot; bgcolor=&quot;#ffffff&quot; width=&quot;150&quot; height=&quot;20&quot; name=&quot;ZeroClipboardMovie_1&quot; align=&quot;middle&quot; allowScriptAccess=&quot;always&quot; allowFullScreen=&quot;false&quot; type=&quot;application/x-shockwave-flash&quot; pluginspage=&quot;http://www.macromedia.com/go/getflashplayer&quot; flashvars=&quot;id=1&amp;width=150&amp;height=20&quot; wmode=&quot;transparent&quot; /&gt;</pre></div></div>

</blockquote>
<p>IE 的 Flash JavaScript 通信接口上有一个 bug 。你必须插入一个 object 标签到一个已存在的 DOM 元素中。并且在写入 innerHTML 之前请确保该元素已经 appendChild 方法插入到 DOM 中。</p>
<h3>Zero Clipboard 事件处理</h3>
<p><strong>Zero Clipboard</strong> 提供了一些事件，你可以自定义函数处理这些事件。Zero Clipboard 事件处理函数为 addEventListener(); 例如当 Flash 完全载入后会触发一个事件 &#8220;load&#8221; 。</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">clip.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;load&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>client<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Flash 加载完毕！&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</blockquote>
<p>Zero Clipboard 会将 clip 对象作为参数传入。即上例中的 &#8220;client&#8221; 。<br />
还有 &#8220;load&#8221; 也可以写成 &#8220;onLoad&#8221;，其他的事件也可以这样。<br />
其他事件还包括：</p>
<p style="text-indent:0;padding-left:30px;">mouseOver 鼠标移上事件<br />
mouseOut 鼠标移出事件<br />
mouseDown 鼠标按下事件<br />
mouseUp 鼠标松开事件<br />
complete 复制成功事件</p>
<p>其中 mouseOver 事件和 complete 事件比较常用。<br />
前面说过，如果需要动态改变待复制的内容，那 mouseOver 事件就可以派上用场了。例如需要动态复制一个 id 为 test 的输入框中的值，我们可以在鼠标 over 的时候重新设置值。</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">clip.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;mouseOver&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>client<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> test <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #660066;">setText</span><span style="color: #009900;">&#40;</span> test.<span style="color: #660066;">value</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 重新设置要复制的值</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//复制成功：</span>
clip.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;complete&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;复制成功！&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</blockquote>
<div class="copyright"><span class="fav"><span class="icon"></span><span><strong>收藏本文到：</strong><a target="_blank" rel="nofollow" href="http://shuqian.qq.com/post?title=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题&#038; uri=http://www.js8.in/407.html" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a target="_blank" rel="nofollow" href="http://www.google.com/bookmarks/mark?op=add&#038;hl=zh_cn&#038;title=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题&#038;bkmk=http://www.js8.in/407.html" title="收藏本文到Google书签" target="_blank">Google书签</a> | <a target="_blank" rel="nofollow" href="http://cang.baidu.com/do/add?it=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题&#038;iu=http://www.js8.in/407.html" title="收藏本文到百度搜藏" target="_blank">百度搜藏</a> | <a target="_blank" rel="nofollow" href=" http://share.renren.com/share/buttonshare.do?link=http://www.js8.in/407.html&#038;title=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题" title="收藏本文到人人网" target="_blank">人人网</a> | <a target="_blank" rel="nofollow" href="http://www.douban.com/recommend/?url=http://www.js8.in/407.html&#038;title=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a target="_blank" rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题&#038;rurl=http://www.js8.in/407.html" title="收藏本文到开心网" target="_blank">开心网</a> | <a target="_blank" rel="nofollow" href="http://myweb.cn.yahoo.com/popadd.html?title=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题&#038;url=http://www.js8.in/407.html" title="收藏本文到Yahoo收藏" target="_blank">Yahoo收藏</a> | <a target="_blank" rel="nofollow" href="http://vivi.sina.com.cn/collect/icollect.php?title=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题&#038;url=http://www.js8.in/407.html" title="收藏到新浪ViVi" target="_blank">新浪ViVi</a> | <a target="_blank" rel="nofollow" href="http://leshou.com/post?act=shou&#038;title=使用ZeroClipboard解决跨浏览器复制到剪贴板的问题&#038;url=http://www.js8.in/407.html" title="收藏本文到乐收" target="_blank">乐收</a></span></span><span><span class="icon"></span><span><strong>声明：</strong>文章未声明为原创文章，本文链接 <a rel="external nofollow" href="http://www.js8.in/407.html" title="使用ZeroClipboard解决跨浏览器复制到剪贴板的问题">http://www.js8.in/407.html</a>. 转载请注明转自<a rel="bookmark inlinks" href="http://www.js8.in" title="Theo Wong来自于青岛普加智能有限公司"> JS8.IN ™</a></span></span></div>
<h3>相关文章</h3>
<ul class="related_posts">
<li><a href="http://www.js8.in/501.html" title="IE中Image.onload失效的问题" rel="bookmark inlinks">IE中Image.onload失效的问题</a></li>
<li><a href="http://www.js8.in/541.html" title="IE onchange事件触发bug？" rel="bookmark inlinks">IE onchange事件触发bug？</a></li>
<li><a href="http://www.js8.in/538.html" title="javascript在光标位置插入内容" rel="bookmark inlinks">javascript在光标位置插入内容</a></li>
<li><a href="http://www.js8.in/337.html" title="jQuery浏览器判断一个bug，以及修改建议" rel="bookmark inlinks">jQuery浏览器判断一个bug，以及修改建议</a></li>
<li><a href="http://www.js8.in/467.html" title="Javascript设置光标位置到结尾并显示出来" rel="bookmark inlinks">Javascript设置光标位置到结尾并显示出来</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.js8.in/407.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
