2011-04-15 71 views
5

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

+1

[Javascript to pre-select textarea on page load](http://stackoverflow.com/questions/3646004/javascript-to-pre-select-textarea-on-page-load) – Neal 2011-04-15 16:53:34

+0

@尼尔感谢您的链接。我没有看到这个问题。 – 2011-04-15 16:56:30

回答

8

JSFiddle Demo

你可以这样来做:

HTML:

<textarea id='mytext'>Testing 1 2 3</textarea> 

的JavaScript:

window.onload = document.getElementById('mytext').select(); 

哪里mytext的内容是你的textarea

-1

这是贴在dupe答案:

$(document).ready(function() { 
    $('#text-area-id').focus(); 
}); 
check here: http://jsfiddle.net/jxrS7/ 
1

试试这个:

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

在你的身体的onload功能,一个电话到你的textarea的选择功能...

HTML :

<body onload='highlightTextArea()'> 
    <textarea id='myTextArea'>Hello World!</textarea> 
</body> 

JS:

var highlightTextArea = function(){ 
    document.getElementById('myTextArea').select(); 
}