javascript在textarea onload中选择文本

问题描述

| 使用JavaScript加载页面时,如何自动选择文本区域中的文本?     

解决方法

JSFiddle演示 您可以这样操作: HTML:
<textarea id=\'mytext\'>Testing 1 2 3</textarea>
JavaScript:
 window.onload = document.getElementById(\'mytext\').select();
Mytext是您的文本区域     ,尝试这个:
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>
    ,在您身体的onload函数中,调用文本区域的select函数。 HTML:
<body onload=\'highlightTextArea()\'>
    <textarea id=\'myTextArea\'>Hello World!</textarea>
</body>
JS:
var highlightTextArea = function (){
    document.getElementById(\'myTextArea\').select();
}
    ,这是在骗子上发布的答案:
$(document).ready(function() {
  $(\'#text-area-id\').focus();
});
check here: http://jsfiddle.net/jxrS7/