简体   繁体   English

自动突出显示焦点上的输入字段

[英]Auto-highlight an input field on focus

I was wondering if there was a way for text inside a input box (pre loaded using value="") to highlight when the user clicks on it? 我想知道输入框内是否存在文本方式(使用value =“”预先加载),以便在用户点击它时突出显示?

input type='text' name='url' id='url' value='http://www.a-link.com/' />

EDIT 编辑

I need the text to he highlighted so the user can copy it. 我需要突出显示的文本,以便用户可以复制它。

<input type="text" name="textbox" value="Test" onclick="this.select()" />

You could attach javascript to the click event to select the text like so: 您可以将javascript附加到click事件以选择文本,如下所示:

$(document).ready( function() {
  $('#id').click( function( event_details ) {
    $(this).select();
  });
});

There is a potential issue where the user could be trying to click at a later point in the text to correct a typing mistake and end up selecting the whole thing. 有一个潜在的问题,用户可能会尝试在文本中稍后点击以更正输入错误并最终选择整个事情。 A better way would be to trigger this when the input gets focus from the user. 更好的方法是在输入从用户获得焦点时触发此操作。 you'd replace .click with .focus in the example above. 你替换.click.focus在上面的例子。

jQuery event documentation: http://api.jquery.com/category/events/ jQuery事件文档: http//api.jquery.com/category/events/

Add the following onclick attribute to make the entire <input> automatically highlight when the user clicks on it: 添加以下onclick属性,以便在用户单击时自动突出显示整个<input>

<input type="text" value="Test1" onclick="this.select()" />

Alternatively, if you want the user to be able to change the selection after the initial click, change the onclick attribute to an onfocus attribute. 或者,如果您希望用户能够在初始单击后更改选择,请将onclick属性更改为onfocus属性。 This will also highlight the entire <input> when the user clicks on it, but it allows them to change the highlighted part manually afterwards: 当用户点击它时,这也会突出显示整个<input> ,但它允许他们之后手动更改突出显示的部分:

<input type="text" value="Test2" onfocus="this.select()" />

Here is an example of both inputs in action. 以下是两个输入实例的示例。

$('#foo').on('mouseup', function(e){
    e.preventDefault();
    $(this).select();
});

$('#foo').on('mouseup', function(e){
    e.preventDefault();
    $(this).select();
});

You want to use focus property. 您想要使用焦点属性。 Like this: http://jsfiddle.net/sCuNs/ 像这样: http//jsfiddle.net/sCuNs/

html HTML

  <p><input type="text" size="40"></p>

css CSS

input:focus, textarea:focus{
background-color: green;
}

Do you mean to select the text? 你的意思是选择文字吗?

Use onclick event to fire the code: 使用onclick事件来触发代码:

document.getElementById("target-input-id").select();

这应该这样做:

<input type='text' name='url' id='url' onclick="this.select()" value='http://www.a-link.com/' />

document.getElementById('inputField').focus(); 的document.getElementById( 'inputField')聚焦();

The default behavior for focus selects the text in the input field. 焦点的默认行为选择输入字段中的文本。 I was looking for a solution not to do that when I found this. 当我发现这个时,我正在寻找一个不要那样做的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM