使用 ZeroClipboard 将 URL 复制到剪贴板
我正在尝试利用 ZeroClipboard (http://code.google.com/p/zeroclipboard/wiki/Instructions) 将当前 URL 复制到用户的剪贴板。我知道我在这里遗漏了一些东西,但我在控制台中没有收到任何类型的错误,也没有工作:
JavaScript
<script src="/js/zero-clipboard.js"></script>
<script>
var clip = null;
ZeroClipboard.setMoviePath( '/ZeroClipboard10.swf' );
function $(id) { return document.getElementById(id); }
function init() {
clip = new ZeroClipboard.Client();
clip.setHandCursor( true );
clip.addEventListener('load', function (client) {
debugstr("Flash movie loaded and ready.");
});
clip.addEventListener('mouseOver', function (client) {
// update the text on mouse over
clip.setText( $('#copyURL').href );
});
clip.addEventListener('complete', function (client, text) {
debugstr("Copied text to clipboard: " + text );
});
clip.glue( 'copyURL', 'copyURLContainer' );
}
function debugstr(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
$('d_debug').appendChild(p);
}
</script>
HTML:
<div id="copyURLContainer">
<a id="copyURL" href="javascript:window.location">COPY URL</a>
</div>
任何想法我在代码中遗漏了什么?
编辑:我还尝试将clip.addEventListener 设置为window.location。那也没用。我可以把 $('#copyURL') 部分取出来吗?
clip.addEventListener('mouseOver', function (client) {
// update the text on mouse over
clip.setText( $('#copyURL').window.location );
});
这个我还没想明白有人对我缺少的东西有什么想法吗?
I'm trying to utilize ZeroClipboard (http://code.google.com/p/zeroclipboard/wiki/Instructions) to copy the current URL to the user's clipboard. I know I'm missing something here, but I am not getting any kind of error in the console, nor is it working as of yet:
JavaScript
<script src="/js/zero-clipboard.js"></script>
<script>
var clip = null;
ZeroClipboard.setMoviePath( '/ZeroClipboard10.swf' );
function $(id) { return document.getElementById(id); }
function init() {
clip = new ZeroClipboard.Client();
clip.setHandCursor( true );
clip.addEventListener('load', function (client) {
debugstr("Flash movie loaded and ready.");
});
clip.addEventListener('mouseOver', function (client) {
// update the text on mouse over
clip.setText( $('#copyURL').href );
});
clip.addEventListener('complete', function (client, text) {
debugstr("Copied text to clipboard: " + text );
});
clip.glue( 'copyURL', 'copyURLContainer' );
}
function debugstr(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
$('d_debug').appendChild(p);
}
</script>
HTML:
<div id="copyURLContainer">
<a id="copyURL" href="javascript:window.location">COPY URL</a>
</div>
Any ideas what I'm missing in my code?
Edit: I also tried making the the clip.addEventListener set to window.location. That didn't work either. Could I pull the $('#copyURL') bit out?
clip.addEventListener('mouseOver', function (client) {
// update the text on mouse over
clip.setText( $('#copyURL').window.location );
});
I still haven't figured this one out. Anyone have any ideas on what I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,当将clip.setText设置为window.location时,它传递了一个对象。必须开始一个空字符串才能正确通过。现在正在工作。
Turns out when setting clip.setText to window.location, it was passing an object. Had to start an empty string in order for it to pass properly. It's working now.