在文本区域字段中设置焦点和突出显示

Setting focus and highlight in a textarea field

本文关键字:显示 焦点 设置 文本 区域 字段      更新时间:2023-09-26

我使用以下命令通过 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>
您可以使用

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>

试试这个:

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

如果页面上有多个文本区域,则需要将 ID 添加到文本区域:

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

并使用getElementById:

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

所以这是实际有效的...

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";

它最终成为响应的组合。感谢大家的线索...