[LayaAirIDE 1.0]Layabox web 复制一段文字到剪贴板

发布h5版本,在微信浏览器上实现复制一段文字到剪贴板
layabox ts版本
实现方法详细步骤(本人没做过web开发,小白一个)
 
function copyText(str) {
//实现...
}
已邀请:

wu语

赞同来自:

问题解决了,在网上找了个自定义弹窗的东西,弹窗的时候,用确定按钮做复制,自定义弹窗参考:https://blog.csdn.net/qq_27437967/article/details/70858262
 
复制到剪贴板代码如下:(窗口有点难看...)
 
function copyText(str) {
new ShowDiv(str);

function ShowDiv(str) { 
var _this = this; 
this.AppConfirm = document.getElementById("AppConfirm");
if (this.AppConfirm == null) {
this.AppConfirm = document.createElement("div");
document.body.appendChild(this.AppConfirm);
this.AppConfirm.id = "AppConfirm";

this.modlueDiv = document.getElementById("modlue");
if (this.modlueDiv == null) {
this.modlueDiv = document.createElement("div");
this.AppConfirm.appendChild(this.modlueDiv);
this.modlueDiv.id = "modlue";

this.MyContent = document.getElementById("MyContent");
if (this.MyContent == null) {
this.MyContent = document.createElement("div");
this.MyContent.id = "MyContent";
this.MyContent.className = "MyContent";
this.modlueDiv.appendChild(this.MyContent);
this.MyContent.innerText = "复制成功";

this.MyBtn = document.getElementById("MyBtn");
if (this.MyBtn == null) {
this.MyBtn = document.createElement("div");
this.MyBtn.id = "MyBtn";
this.MyBtn.className = "MyBtn";
this.modlueDiv.appendChild(this.MyBtn);

this.confirmBtn = document.getElementById("confirm");
if (this.confirmBtn == null) {
this.confirmBtn = document.createElement("button");
this.confirmBtn.id = "confirm";
this.confirmBtn.innerText = "确定"
this.MyBtn.appendChild(this.confirmBtn);
}
this.newMask = document.createElement("div"); 
this.modlueDiv.style.visibility = "visible"; 
var clipboard = new ClipboardJS("#confirm", {
text: function () {
return str;
}
}) 
clipboard.on("success", function (e) {
console.log("ok:")
console.log(e)
clipboard.destroy();
})
clipboard.on("error", function (e) {
console.log("err:")
console.log(e)
clipboard.destroy();
}) 
//确认按钮
this.confirmBtn.onclick = function () {
_this.CloseDivAndInfoMesg(this);
}
// 创建弹出层 遮罩层 等
if (!document.getElementById("mask") && 1) {
//mask div
this.newMask.id = "mask";
this.newMask.style.position = "absolute";
this.newMask.style.zIndex = "1";
this.newMask.style.width = "100%";
this.newMask.style.height = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight) + 100 + "px";
this.newMask.style.top = "0px";
this.newMask.style.left = "0px";
this.newMask.style.background = "gray";
this.newMask.style.filter = "alpha(opacity=80)";
this.newMask.style.opacity = "0.5";
document.body.appendChild(this.newMask);
}
_this.mask = document.getElementById("mask");
_this.mask.style.visibility = "visible";
}
//点击取消按钮关闭模态框
ShowDiv.prototype.CloseDiv = function () { 
this.modlueDiv.style.visibility = "hidden";
this.mask.style.visibility = "hidden";
}
//点击确认按钮关闭模态框,提示增加信息
ShowDiv.prototype.CloseDivAndInfoMesg = function () {
this.CloseDiv();
}

王壮壮

赞同来自:

/** 复制文本
     *  @param str 需要复制的内容
     *  @param isAlertMsgId 需要弹出提示的信息ID,23为复制成功
     *  @param isAlertMsgInfo 需要提示的具体内容
     *  */
    public static copy(str: string ,isAlertMsgId?:number ,isAlertMsgInfo?:string): boolean{
        let re: boolean;
        try {
            let input = document.createElement("input");
            input.value = str;
            document.body.appendChild(input);
            input.select();
            input.setSelectionRange(0, input.value.length),
                document.execCommand('Copy');
            document.body.removeChild(input);
            let msg: string = isAlertMsgInfo;
            re = true;
        } catch (e) {
            console.error("Current equipment support Copy!");
        }
        return re;
    }

熊猫大侠

赞同来自:

inputDiv = Browser.document.createElement("input");  
inputDiv.type = "button";
inputDiv.id = "btn";
inputDiv.className = "btn";
inputDiv.value = "复制按钮";
inputDiv.style.width="138px";
inputDiv.style.height="50px";
inputDiv.style.opacity = "0";
inputDiv.style.filter = "alpha(opacity=0);";
inputDiv.setAttribute("data-clipboard-text","我是被复制的内容啊");
inputDiv.style.position ="absolute";
inputDiv.style.top = “0px";
inputDiv.style.left = “0px";
Browser.container.appendChild(inputDiv);
 
var btnElem:* = Browser.document.getElementById('btn');
var clipboard:* = __JS__("new ClipboardJS(btnElem)");
 
//可以自己加些处理
clipboard.on('success', function(e):* {
rightClickBox.visible = false;
if(inputDiv && inputDiv.parentNode)
{
Browser.container.removeChild(inputDiv);
}
});
clipboard.on('error', function(e):* {
});把这个按钮放在自己的复制按钮之上 自己去算自己的位置 电脑浏览器是可以复制粘贴的 手机浏览器没试过

该问题目前已经被锁定, 无法添加新回复

商务合作
商务合作