gpt4 book ai didi

javascript - 尝试通过单击按钮从文本区域复制代码

转载 作者:行者123 更新时间:2023-11-29 10:55:56 25 4
gpt4 key购买 nike

我正在尝试添加一段代码,让我的用户从 textarea 复制一些链接。非常基本,虽然我在尝试粘贴时得到成功响应,但我发现它没有复制它。我错过了什么?

<textarea id="shareInfo" class="form-control" rows="10" disabled="disabled"><a href="'.$pgURLcanon.'"><img src="'.$meta_image.'" alt="'.$_SESSION['articles'][$article_seo]['title'].'"></a>

</textarea>
<button onclick="copyInfoCode()" class="btn btn-success mt-2">Copy code</button>

<script>
function copyInfoCode() {
$("#shareInfo").select();

try {
var success = document.execCommand("copy");
console.log("Copying " + (success ? "Code copied successfully." : "Copy failed, please try again"));
} catch (err) {
console.log("Copying failed");
}
}
</script>

最佳答案

execCommand 将无法在禁用的文本区域上工作。而不是禁用使用只读。

function copyInfoCode() {
$("#shareInfo").select();

try {
var success = document.execCommand("copy");
console.log("Copying " + (success ? "Code copied successfully." : "Copy failed, please try again"));
} catch (err) {
console.log("Copying failed");
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<textarea id="shareInfo" class="form-control" rows="10" readonly><a href="'.$pgURLcanon.'"><img src="'.$meta_image.'" alt="'.$_SESSION['articles'][$article_seo]['title'].'"></a>
</textarea>
<button onclick="copyInfoCode()" class="btn btn-success mt-2">Copy code</button>

关于javascript - 尝试通过单击按钮从文本区域复制代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57259346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com