javascript javascript在textarea中选择文本onload
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5679932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript select text in textarea onload
提问by Web_Designer
How do I automatically select the text that is in a textarea when the page loads using JavaScript?
使用 JavaScript 加载页面时,如何自动选择 textarea 中的文本?
回答by u476945
You can do it this way:
你可以这样做:
HTML:
HTML:
<textarea id='mytext'>Testing 1 2 3</textarea>
JavaScript:
JavaScript:
window.onload = document.getElementById('mytext').select();
Where mytext is your textarea
其中 mytext 是您的 textarea
回答by Anish
Try this:
试试这个:
Textarea:
<textarea rows="3" id="txtarea" style="width:200px" >This text you can select all by clicking here </textarea>
<script type="text/javascript">
document.getElementById('txtarea').focus();
document.getElementById('txtarea').select();
</script>
回答by Mark Costello
In the onload function of your body, place a call to the select function of your textarea...
在你的body的onload函数中,调用你的textarea的select函数...
HTML:
HTML:
<body onload='highlightTextArea()'>
<textarea id='myTextArea'>Hello World!</textarea>
</body>
JS:
JS:
var highlightTextArea = function (){
document.getElementById('myTextArea').select();
}