[英]Using execCommand (Javascript) to copy hidden text to clipboard
[英]Make execCommand(“Copy”) work on hidden input tags
我目前正在使用用javascript硬編碼的功能,以便復制標記的自定義地址和預定義的鏈接。 我所做的是制作一個輸入類型文本元素,並將其值設置為我想要復制的內容,但是事實證明,它並沒有按照我想要的方式工作。 按下Ctrl + V后,它將第二個粘貼到設備剪貼板中的最新值。 如何使execCommand(“ Copy”)在隱藏的輸入標簽上工作? (該函數在img標簽的onclick屬性上調用)
function shareFile() {
var hiddenItem = document.createElement("input");
hiddenItem.type = "text";
hiddenItem.setAttribute("style","display: none");
hiddenItem.value = <?php echo (isset($_SERVER['HTTPS'])?"\"https://":"\"http://").$_SERVER['SERVER_NAME']."/filesystem/openfile.php?dir=\" + " ?> encodeURI(this.parentNode.parentNode.parentNode.getAttribute("r-directory"));
hiddenItem.select();
console.log(hiddenItem.value);
document.execCommand("Copy");
// location.href = "resources.php?smsg=The file's link has been successfully copied to your clipboard." <?php if (!empty($_GET['dir'])) echo " + \"&dir=".$_GET['dir']."\""?>;
return false;
}
PS盡可能沒有jQuery請! 謝謝。
execCommand適用於添加到document元素任何部分的那些元素。
該代碼缺少document.querySelector("body").appendChild(hiddenItem);
並且可以在執行命令-> hiddenItem.setAttribute("style","display: none");
之后隱藏hiddenItem.setAttribute("style","display: none");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.