![](/img/trans.png)
[英]Copy span text to input field automatically when page load using javascript
[英]Automatically copy text when clicked on input area (multiple lnputs) Javascript
我正在嘗試獲取輸入區域以在用戶單擊它時復制文本。 代碼本身正在選擇正確的文本,但是當我添加復制功能時,它不會復制正確的文本。 問題是這是網站用來向網站添加元素的模板(因此,多個提交將使用相同的代碼)。
echo '<div class="input-group input-group-sm">';
echo '<span class="input-group-addon input-label-' . strtolower($server->status_text) . '"><i class="fa fa-server" aria-hidden="true"></i> ' . $server->status_text . '</span>';
echo '<input type="text" onclick="this.select()" value="' . $server->address . (($server->connection_port != '25565') ? ":" . $server->connection_port : null) . '">';echo '</div>';
現場輸出:
<div class="input-group input-group-sm">
<span class="input-group-addon input-label-online"><i class="fa fa-server" aria-hidden="true"></i> Online</span>
<input type="text" onclick="this.select()" class="form-control" value="Server IP">
</div>
編輯:看來我將需要通過按鈕來執行此操作,因為使用“ onclick”會使我的網站崩潰。 不知道如何在沒有某種ID系統的情況下使用按鈕進行操作。 如果有幫助,則通過“表”列出服務器。
這應該為您工作:
<input type="text" onclick="this.select(); document.execCommand('Copy');" class="form-control" value="Server IP">
您嘗試使用的代碼沒有轉義的括號。
這適用於多個輸入元素: https : //jsfiddle.net/xx0gs6Lw/
編輯
那只能是語法錯誤,那么您可以發布確切的代碼嗎? 它看起來應該像這樣:
echo '<input type="text" onclick="this.select(); document.execCommand(\'Copy\');" value="' . $server->address . (($server->connection_port != '25565') ? ":" . $server->connection_port : null) . '">';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.