将文本复制到剪贴板的最佳方法是什么?(多浏览器)
我试过了:
function copyToClipboard(text) {
if (window.clipboardData) { // Internet Explorer
window.clipboardData.setData("Text", text);
} else {
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(text);
}
}
Run Code Online (Sandbox Code Playgroud)
但在Internet Explorer中,它会出现语法错误.在Firefox中,它说unsafeWindow is not defined.
没有闪存的好技巧:Trello如何访问用户的剪贴板?
在Internet Explorer中,我可以使用clipboardData对象来访问剪贴板.我怎样才能在FireFox,Safari和/或Chrome中执行此操作?
在此代码中,createRange无法在Chrome中使用.在IE中,它正在运行.请帮忙解决这个问题.是否有任何其他属性可以像创建范围一样工作.这样对我的项目很有帮助.
<script language=javascript>
var isSelected;
function markSelection ( txtObj ) {
if ( txtObj.createTextRange ) {
txtObj.caretPos = document.selection.createRange().duplicate();
isSelected = true;
}
}
function insertTag ( txtName, enclose ) {
if(document.f_activity_email == null) {
var tag = document.getElementById('EmailTokenID').value;
}
else {
var formC = document.f_activity_email;
var tag = formC.EmailTokenID.value;
}
var closeTag = tag;
if ( enclose ) {
var attribSplit = tag.indexOf ( ' ' );
if ( tag.indexOf ( ' ' ) > -1 ) …Run Code Online (Sandbox Code Playgroud) 这是我用来显示一些数据的代码。在这段代码中,我有锚标签,我想在点击它时复制该锚标签的链接。这是我使用的代码如下:
<div class="search_item_list clearfix" id="response">
<?php foreach($jobs as $job){
?>
<a class="copy_text" data-toggle="tooltip" title="Copy to Clipboard"
href="<?=base_url().'home/company_profile_detail?id='.$job['company_id'];?>"><span class="icon link"><i class="fa fa-link"></i></span>Copy Link</a>
<?php } ?>
</div>
<script>
$(".copy_text").click(function(e){
e.preventDefault();
var button = $(this);
var text = button.attr("href");
text.select();
$(document).execCommand("copy");
alert("Copied the text ");
})
</script>
Run Code Online (Sandbox Code Playgroud)
我得到 jQuery 作为
text.select 不是函数。