复制到剪贴板在 FireFox 上不起作用

发布于 2024-08-18 04:07:15 字数 910 浏览 1 评论 0原文

我已经实现了复制到剪贴板功能。它在 IE 上的所有版本上都能正常工作,但在 FireFox 上却无法工作。请帮我解决这个问题。 详细信息是

<script src="../../Scripts/JQPlugins/jquery.clipboard.js" type="text/javascript"></script>

<script src="../../Scripts/JQPlugins/jquery.clipboard.pack.js" type="text/javascript"></script>   
 <script type="text/javascript">
        $.clipboardReady(function() {
            $("input#buttonid").bind('click', function() {
                var text = $("#url").attr("href") + "\n" + $("#pwd").html();
                $.clipboard(text);
                alert("hi");
                return false;
            });
        }, { swfpath: "../../Scripts/JQPlugins/jquery.clipboard.swf", debug: true }); 

    </script> 

我的代码文件结构是

Project > Scripts > JQPlugins > 
1. jquery.clipboard.js
2. jquery.clipboard.pack.js
3. jquery.clipboard.swf

I had implemented copy to clipboard functionality. It is working fine with all version on IE but not working in FireFox. Please help me solve out this problem.
Detail are

<script src="../../Scripts/JQPlugins/jquery.clipboard.js" type="text/javascript"></script>

<script src="../../Scripts/JQPlugins/jquery.clipboard.pack.js" type="text/javascript"></script>   
 <script type="text/javascript">
        $.clipboardReady(function() {
            $("input#buttonid").bind('click', function() {
                var text = $("#url").attr("href") + "\n" + $("#pwd").html();
                $.clipboard(text);
                alert("hi");
                return false;
            });
        }, { swfpath: "../../Scripts/JQPlugins/jquery.clipboard.swf", debug: true }); 

    </script> 

And my code file structure is

Project > Scripts > JQPlugins > 
1. jquery.clipboard.js
2. jquery.clipboard.pack.js
3. jquery.clipboard.swf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-08-25 04:07:15

将文本复制到剪贴板的方法是 jQuery 剪贴板插件;它使用 IE 的本机功能将文本复制到剪贴板,但在非 IE 平台上使用 Flash 插件。我敢打赌,您的计算机上有 Flash 10,或者至少在您测试 Firefox 的计算机上 — Flash 10 不再允许在没有明确用户请求的情况下进行剪贴板操作,这可能就是您看到它无法运行的原因火狐。 (甚至在插件的发行说明中提到了这一点。)

话虽这么说,人们已经已经找到了安全限制的解决方法;解决方法包括将不可见的 Flash 影片放置在您选择的 DOM 元素之上,这意味着当用户单击或以其他方式调用该元素来提交数据时,Flash 影片会注册用户采取了显式操作,并允许剪贴板操作。 Zero Clipboard 就是一个提供解决方法的库,但如果您寻找的话,还有其他库他们。

The method you're using to copy text to the clipboard is the jQuery clipboard plugin; it uses IE's native abilities to copy text to the clipboard, but uses a Flash plugin on non-IE platforms. My bet is that you have Flash 10 on your machine, or at least on the machine you're testing Firefox on -- Flash 10 no longer allows clipboard manipulation without explicit user request, which is likely why you're seeing it not work on Firefox. (This is even mentioned in the release notes for the plugin.)

That being said, people have already figured out a workaround for the security restrictions; the workaround involves placing an invisible Flash movie atop the DOM element of your choice, which means that when the user clicks or otherwise invokes the element to submit data, the Flash movie registers that the user took explicit action, and allows the clipboard manipulation. Zero Clipboard is one such library providing the workaround, but there are others out there if you look for 'em.

墟烟 2024-08-25 04:07:15
please try the below script for all browsers: chrome/Edge/Firefox

将 id 传递给函数: selectElementContents(document.getElementById("printtext"));

 function selectElementContents(el) {
 var body = document.body, range, sel;
 $(el).attr("contenteditable", true);
 if (document.createRange && window.getSelection) {
     range = document.createRange();
     sel = window.getSelection();
     sel.removeAllRanges();
     try {
         range.selectNodeContents(el);
         sel.addRange(range);
     } catch (e) {
         range.selectNode(el);
         sel.addRange(range);
     }
 } else if (body.createTextRange) {
     range = body.createTextRange();
     range.moveToElementText(el);
     range.select();
 }
 document.execCommand("Copy");
 window.getSelection().removeAllRanges();
 $(this).removeAttr("contenteditable");
 $(el).attr("contenteditable", false);
}
please try the below script for all browsers: chrome/Edge/Firefox

Pass the id to the functions : selectElementContents(document.getElementById("printtext"));

 function selectElementContents(el) {
 var body = document.body, range, sel;
 $(el).attr("contenteditable", true);
 if (document.createRange && window.getSelection) {
     range = document.createRange();
     sel = window.getSelection();
     sel.removeAllRanges();
     try {
         range.selectNodeContents(el);
         sel.addRange(range);
     } catch (e) {
         range.selectNode(el);
         sel.addRange(range);
     }
 } else if (body.createTextRange) {
     range = body.createTextRange();
     range.moveToElementText(el);
     range.select();
 }
 document.execCommand("Copy");
 window.getSelection().removeAllRanges();
 $(this).removeAttr("contenteditable");
 $(el).attr("contenteditable", false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文