HTML textbox, auto highlighting text

2019-06-23 02:24发布

问题:

How would I make a textbox, containing preexisting text, that when the user clicked within it all the text inside it would become highlighted. For example, the same way YouTube does the textboxes for the embed code on their videos. Thanks

回答1:

If I've understood your problem correctly, you could use some javascript (untested code):

<script language="JavaScript">
  function selectText(textField) 
  {
    textField.focus();
    textField.select();
  }
</script>

<input type="text" name="sometext" size="100" value="The Text" onClick='selectText(this);'>

You can put the script between your <head> and </head> tags.