簡體   English   中英

在文本區域中設置焦點和突出顯示

[英]Setting focus and highlight in a textarea field

我正在使用命令顯示通過PHP輸入的textarea

print " '<textarea rows='16' cols='30'>$flist'</textarea><BR>";

我希望textarea可以自動選擇焦點和內容$flist 到目前為止,我所發現的只是如何在單擊輸入時進行選擇。

我將如何使用javascript(無jquery)完成此操作?

謝謝,沃爾特

編寫如下代碼:

// Assign a id to textarea and rewrite below line :-
print "<textarea rows='16' cols='30'>$flist</textarea><BR>";

在Js中使用getElementById :-

<script>
function setFocusToTextBox(){
   document.getElementById("mytext").focus();
}
</script>

嘗試這個:

var textarea = document.getElementsByTagName("textarea")[0];
textarea.focus();
textarea.select();

如果頁面上有多個textarea,則需要將id添加到textarea:

print "<textarea id='text' rows='16' cols='30'>$flist</textarea><BR>";

並使用getElementById:

var textarea = document.getElementById("text");

您可以使用focus()select()

document.querySelector('textarea').focus();
document.querySelector('textarea').select();

您的PHP代碼應為:

print "<textarea rows='16' cols='30'>$flist</textarea><BR>";

希望這可以幫助。


 document.querySelector('textarea').focus(); document.querySelector('textarea').select(); 
 <textarea id='text' rows='16' cols='30'>text here</textarea><BR> 

所以這是實際起作用的...

print " <textarea id='txarea' rows='16' cols='30'>$flist</textarea><BR>";
print "     <script>\n";
print "        document.getElementById('txarea').focus();\n";
print "        document.getElementById('txarea').select();\n";
print "     </script>\n";

最終是這些回應的結合。 感謝所有線索...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM