此示例显示如何使用javaScript单击一次选择整个输入。
语法如下:
<input onClick="this.select();" >
or
document.getElementById("ID").select();
示例1:使用" this.select()".
<!DOCTYPE html>
<html>
<head>
<title>
Select all text in HTML
text input when clicked
</title>
</head>
<body>
<br> Input Text:
<br>
<input onClick= "this.select();"
type= "text"
value= "lsbin" >
</body>
</html>
输出如下:
点击之前:

单击后:

示例2:使用DOM输入文字select()方法
<!DOCTYPE html>
<html>
<head>
<title>
Select all text in HTML
text input when clicked
</title>
</head>
<body>
Input Text:
<br>
<input type= "text"
value= "lsbin"
id= "eid" >
<button type= "button"
onclick= "myFunction()" >
Try it
</button>
<script>
function myFunction() {
document.getElementById(
"eid" ).select();
}
</script>
</body>
</html>
输出如下:
点击之前:

单击后:
