将 url 复制到剪贴板后显示成功消息
display success message after copying url to clipboard
我正在尝试在将 url 复制到剪贴板后显示一条消息(即“已复制!”)。你能协助我如何继续吗?
我从 Whosebug 和 codepen 研究了这个:, 2, 3 等等。
function copy(){var e=document.getElementById("box");e.value=window.location.href,e.focus(),e.select(),document.getElementById("ctooltip").style.display="inline",document.execCommand("copy"),setTimeout(function(){document.getElementById("ctooltip").style.display="none"},2000)}
.copybutton{background-color:transparent;border:0;outline:0;cursor:pointer;opacity:1;position:absolute}.btooltip{display:flex;align-items:center;margin-top:6px;min-height:30px}#ctooltip{display:none;font-size:20px;margin-left:0;padding:3px 8px;background:linear-gradient(45deg,#2e23ac 0,#ee71f9 100%);border-radius:24px;color:#fff;z-index:1;position:relative}.visuallyhidden{position:absolute;clip:rect(1px,1px,1px,1px)}
<div class="btooltip"><button title="copy share link" type="submit" value="copy" onclick="copy();" class=copybutton><svg width="22" height="22" viewBox="0 0 20 20" version="1.1"><path stroke=white stroke-width=".4" fill="#000" d="M20,18.2V19H0v-1.5h16.4h2.1V5.2H20V18.2z M1.5,11.3V5.2H0v12.3h1.5V11.3z M9.2,12.8V0.5h1.5v12.3H9.2z M4.6,5.2h1.5v1.5H4.6V5.2z M6.2,3.6h1.5v1.5H6.2V3.6z M13.8,5.2h1.5v1.5h-1.5V5.2z M12.3,3.6h1.5v1.5h-1.5V3.6z M10.8,2.1h1.5v1.5h-1.5V2.1zM7.7,2.1h1.5v1.5H7.7V2.1z"></path></svg></button><span id="ctooltip">✓</span></div><textarea class=visuallyhidden id="box"></textarea>
将 alert
添加到您的复制功能。
function copy(){
var Url = document.getElementById("box");
Url.value = window.location.href;
Url.focus();
Url.select();
document.execCommand("copy");
alert("Copied!");
}
function copy() {
var Url = document.getElementById("box");
Url.value = window.location.href;
Url.focus();
Url.select();
document.execCommand("copy");
alert("Copied!");
}
.visuallyhidden {
position: absolute;
clip: rect(1px, 1px, 1px, 1px)
}
.copybutton {
background-color: #fff;
border: 0;
outline: 0;
cursor: pointer;
opacity: 1;
position: absolute;
right: 50px;
width: 40px;
height: 40px;
z-index: 9;
border-radius: 24px
}
<button title="copy share link" type="submit" value="copy" onclick="copy();" class=copybutton><svg width="22" height="22" viewBox="0 0 20 20" version="1.1"><path fill="#000" d="M20,18.2V19H0v-1.5h16.4h2.1V5.2H20V18.2z M1.5,11.3V5.2H0v12.3h1.5V11.3z M9.2,12.8V0.5h1.5v12.3H9.2z M4.6,5.2h1.5v1.5H4.6V5.2z M6.2,3.6h1.5v1.5H6.2V3.6z M13.8,5.2h1.5v1.5h-1.5V5.2z M12.3,3.6h1.5v1.5h-1.5V3.6z M10.8,2.1h1.5v1.5h-1.5V2.1zM7.7,2.1h1.5v1.5H7.7V2.1z"></path></svg></button>
<textarea class=visuallyhidden id="box"></textarea>
我为显示 "copied!" 消息创建了一个工具提示:
function copy(){
var Url = document.getElementById("box");
Url.value = window.location.href;
Url.focus();
Url.select();
document.getElementById("custom-tooltip").style.display = "inline";
document.execCommand("copy");
setTimeout( function() {
document.getElementById("custom-tooltip").style.display = "none";
}, 1000);
};
.visuallyhidden{position:absolute;clip:rect(1px,1px,1px,1px)}
.copybutton{background-color:#fff;border:0;outline:0;cursor:pointer;opacity:1;position:absolute;width:40px;height:40px;z-index:9;border-radius:24px}
.button-tooltip-container {
display: flex;
align-items: center;
margin-top: 16px;
min-height: 30px;
}
#custom-tooltip {
display: none;
margin-left: 40px;
padding: 5px 12px;
background-color: #000000df;
border-radius: 4px;
color: #fff;
}
<div class="button-tooltip-container">
<button title="copy share link" type="submit" value="copy" onclick="copy();" class=copybutton><svg width="22" height="22" viewBox="0 0 20 20" version="1.1"><path fill="#000" d="M20,18.2V19H0v-1.5h16.4h2.1V5.2H20V18.2z M1.5,11.3V5.2H0v12.3h1.5V11.3z M9.2,12.8V0.5h1.5v12.3H9.2z M4.6,5.2h1.5v1.5H4.6V5.2z M6.2,3.6h1.5v1.5H6.2V3.6z M13.8,5.2h1.5v1.5h-1.5V5.2z M12.3,3.6h1.5v1.5h-1.5V3.6z M10.8,2.1h1.5v1.5h-1.5V2.1zM7.7,2.1h1.5v1.5H7.7V2.1z"></path></svg></button>
<span id="custom-tooltip">copied!</sapn>
</div>
<textarea class=visuallyhidden id="box"></textarea>
希望对你有用!
我正在尝试在将 url 复制到剪贴板后显示一条消息(即“已复制!”)。你能协助我如何继续吗?
我从 Whosebug 和 codepen 研究了这个:
function copy(){var e=document.getElementById("box");e.value=window.location.href,e.focus(),e.select(),document.getElementById("ctooltip").style.display="inline",document.execCommand("copy"),setTimeout(function(){document.getElementById("ctooltip").style.display="none"},2000)}
.copybutton{background-color:transparent;border:0;outline:0;cursor:pointer;opacity:1;position:absolute}.btooltip{display:flex;align-items:center;margin-top:6px;min-height:30px}#ctooltip{display:none;font-size:20px;margin-left:0;padding:3px 8px;background:linear-gradient(45deg,#2e23ac 0,#ee71f9 100%);border-radius:24px;color:#fff;z-index:1;position:relative}.visuallyhidden{position:absolute;clip:rect(1px,1px,1px,1px)}
<div class="btooltip"><button title="copy share link" type="submit" value="copy" onclick="copy();" class=copybutton><svg width="22" height="22" viewBox="0 0 20 20" version="1.1"><path stroke=white stroke-width=".4" fill="#000" d="M20,18.2V19H0v-1.5h16.4h2.1V5.2H20V18.2z M1.5,11.3V5.2H0v12.3h1.5V11.3z M9.2,12.8V0.5h1.5v12.3H9.2z M4.6,5.2h1.5v1.5H4.6V5.2z M6.2,3.6h1.5v1.5H6.2V3.6z M13.8,5.2h1.5v1.5h-1.5V5.2z M12.3,3.6h1.5v1.5h-1.5V3.6z M10.8,2.1h1.5v1.5h-1.5V2.1zM7.7,2.1h1.5v1.5H7.7V2.1z"></path></svg></button><span id="ctooltip">✓</span></div><textarea class=visuallyhidden id="box"></textarea>
将 alert
添加到您的复制功能。
function copy(){
var Url = document.getElementById("box");
Url.value = window.location.href;
Url.focus();
Url.select();
document.execCommand("copy");
alert("Copied!");
}
function copy() {
var Url = document.getElementById("box");
Url.value = window.location.href;
Url.focus();
Url.select();
document.execCommand("copy");
alert("Copied!");
}
.visuallyhidden {
position: absolute;
clip: rect(1px, 1px, 1px, 1px)
}
.copybutton {
background-color: #fff;
border: 0;
outline: 0;
cursor: pointer;
opacity: 1;
position: absolute;
right: 50px;
width: 40px;
height: 40px;
z-index: 9;
border-radius: 24px
}
<button title="copy share link" type="submit" value="copy" onclick="copy();" class=copybutton><svg width="22" height="22" viewBox="0 0 20 20" version="1.1"><path fill="#000" d="M20,18.2V19H0v-1.5h16.4h2.1V5.2H20V18.2z M1.5,11.3V5.2H0v12.3h1.5V11.3z M9.2,12.8V0.5h1.5v12.3H9.2z M4.6,5.2h1.5v1.5H4.6V5.2z M6.2,3.6h1.5v1.5H6.2V3.6z M13.8,5.2h1.5v1.5h-1.5V5.2z M12.3,3.6h1.5v1.5h-1.5V3.6z M10.8,2.1h1.5v1.5h-1.5V2.1zM7.7,2.1h1.5v1.5H7.7V2.1z"></path></svg></button>
<textarea class=visuallyhidden id="box"></textarea>
我为显示 "copied!" 消息创建了一个工具提示:
function copy(){
var Url = document.getElementById("box");
Url.value = window.location.href;
Url.focus();
Url.select();
document.getElementById("custom-tooltip").style.display = "inline";
document.execCommand("copy");
setTimeout( function() {
document.getElementById("custom-tooltip").style.display = "none";
}, 1000);
};
.visuallyhidden{position:absolute;clip:rect(1px,1px,1px,1px)}
.copybutton{background-color:#fff;border:0;outline:0;cursor:pointer;opacity:1;position:absolute;width:40px;height:40px;z-index:9;border-radius:24px}
.button-tooltip-container {
display: flex;
align-items: center;
margin-top: 16px;
min-height: 30px;
}
#custom-tooltip {
display: none;
margin-left: 40px;
padding: 5px 12px;
background-color: #000000df;
border-radius: 4px;
color: #fff;
}
<div class="button-tooltip-container">
<button title="copy share link" type="submit" value="copy" onclick="copy();" class=copybutton><svg width="22" height="22" viewBox="0 0 20 20" version="1.1"><path fill="#000" d="M20,18.2V19H0v-1.5h16.4h2.1V5.2H20V18.2z M1.5,11.3V5.2H0v12.3h1.5V11.3z M9.2,12.8V0.5h1.5v12.3H9.2z M4.6,5.2h1.5v1.5H4.6V5.2z M6.2,3.6h1.5v1.5H6.2V3.6z M13.8,5.2h1.5v1.5h-1.5V5.2z M12.3,3.6h1.5v1.5h-1.5V3.6z M10.8,2.1h1.5v1.5h-1.5V2.1zM7.7,2.1h1.5v1.5H7.7V2.1z"></path></svg></button>
<span id="custom-tooltip">copied!</sapn>
</div>
<textarea class=visuallyhidden id="box"></textarea>
希望对你有用!