4 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
好的,我對(duì)您的代碼做了一些調(diào)整,但它現(xiàn)在應(yīng)該可以工作了。
<!DOCTYPE HTML>
<html dark= "true" style="font-size: 50px;font-family: Roboto, Arial, sans-serif">
<body>
<textarea id="copytext">ROGER</textarea>
<div class="button" id="adadad">
<button onclick="copyS()" id="dlld">Copy text</button>
</div>
<p> </p>
<!-- removed the single " at the end of your script tag -->
<script type=text/javascript>
//added () after you declared your function
function copyS(){
clicked = document.getElementById("dlld");
//added .id to clicked. Not sure why you need to do this, but it works now
if ("dlld" == clicked.id){
var am1 = document.getElementById("copytext");
//you need to select before running the .execCommand
am1.select();
document.execCommand("copy");
}
}
</script>
<p> </p>
</body>
</html>

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
看來(lái)您需要select()首先從文本區(qū)域獲取文本。嘗試將您的功能更改為:
function copyS{
var clicked = document.getElementById("dlld");
//if ("dlld" == clicked){ //Not sure you need this
var am1 = document.getElementById("copytext");
am1.select();
document.execCommand("copy");
//}
}
希望有幫助!

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
我已經(jīng)更改了您的代碼來(lái)完成這項(xiàng)工作。
var copiedText = "";
document.querySelector("button").onclick = function(e) {
copiedText = e.target.previousElementSibling.value;
document.execCommand("copy");
}
document.body.oncopy = function(e) {
event.clipboardData.setData('text/plain', copiedText);
event.preventDefault();
};
body {
font-size: 50px;
font-family: Roboto, Arial, sans-serif;
}
<textarea>ROGER</textarea>
<button>Copy</button>

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試這個(gè)
(<button id="demo" onclick="copyToClipboard(document.getElementById('demo').innerHTML)">This is what I want to copy</button>)
(<script>
function copyToClipboard(text) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
}
</script>)
- 4 回答
- 0 關(guān)注
- 241 瀏覽
添加回答
舉報(bào)