在textarea字段中设置焦点和突出显示

时间:2016-02-03 10:10:05

标签: javascript php html textarea

我正在使用以下命令通过PHP显示textarea输入:

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

我希望textarea能够自动选择焦点和内容$flist。我迄今发现的所有内容都是如何选择点击输入的时间。

我如何使用javascript(没有jquery)来完成这个?

谢谢, 沃尔特

4 个答案:

答案 0 :(得分:0)

编写如下代码: -

// 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>

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

您可以使用focus()select()

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

您的PHP代码应为:

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

希望这有帮助。

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

答案 3 :(得分:0)

所以这是实际工作的......

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

它最终成为了回应的组合。 感谢所有的线索......

相关问题