简体   繁体   English

Javascript:在当前关注的输入字段中选择文本

[英]Javascript: select the text in the currently focussed input field

I would like to select the text of the input field which currently has focus, so when the user starts typing, the text which is currently in the input field is gone. 我想选择当前具有焦点的输入字段的文本,因此当用户开始键入内容时,当前输入字段中的文本消失了。 Can I do this with javascript (or jquery)? 我可以使用javascript(或jquery)吗?

In case the field which currently has focus is not an input field, nothing has to happen. 如果当前具有焦点的字段不是输入字段,则无需进行任何操作。

thanks! 谢谢!

EDIT: To all the answerers: you all misunderstood (so probably my question was not clear). 编辑:对所有答复者:你们所有人都误解了(所以可能我的问题不清楚)。 I don't want to select the text on the moment the input field gets the focus. 我不想在输入字段获得焦点时立即选择文本。 I would like to have a javascript function which selects the text of the input field which has the focus at that moment. 我想要一个javascript函数,该函数选择当时具有焦点的输入字段的文本。

Sorry I misunderstood what you were looking for I think that I have a better understanding of it now. 抱歉,我误解了您的期望,我认为我现在对此有了更好的了解。 Does this do more of what you were looking to acheive? 这是否可以帮助您实现更多目标?

//If not using 1.7 for jquery you can use bind
$('input, textarea').on({
    focusin: function(){
        $(this).addClass("focused");
    },
    focusout: function(){
        $(this).removeClass("focused");
    }
});

function highlightText()
{
    var focused = $('input.focused,textarea.focused');

    if (focused.size()) {
        focused.get(0).select();   
    }
}

http://jsfiddle.net/GXFpR/1/ http://jsfiddle.net/GXFpR/1/

You can try something like this? 您可以尝试这样的事情吗? http://jsfiddle.net/z55UZ/ http://jsfiddle.net/z55UZ/

 $("input, textarea").focus(
     function()
     {
         this.select();
     }
  )

EDIT: 编辑:

here is an updated fiddle: http://jsfiddle.net/IrvinDominin/z55UZ/2/ 这是更新的小提琴: http : //jsfiddle.net/IrvinDominin/z55UZ/2/

In the example is selected the last focused element; 在示例中,选择了最后一个焦点元素; but if you look at the code the var childInputHasFocus and whoHasFocus are never setted to false...when you want to stop the selecting feature? 但是,如果您查看代码,则永远不会将var childInputHasFocus和whoHasFocus设置为false ...当您要停止选择功能时?

Where you wanna call the function? 您想在哪里调用该函数? Because the click event sets the active/focused element as is caller. 因为click事件将激活/集中的元素设置为呼叫者。

You can check with onfocus and then use this.select() as inline 您可以使用onfocus进行检查,然后使用this.select()作为内联

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

UPDATE: a more universal approach, will add focus to inputs of the text type also input text's that are not readonly 更新:一种更通用的方法,将重点放在文本类型的输入上,也将输入非只读的文本

  window.onload = setFocus(); 

or call beneth the last form input field 或在最后一个表单输入字段之前调用

  setFocus(); 

main 主要

setFocus = function(){
  var i = [];
  i = document.getElementsByTagName("input");
    for(var t=0; t < i.length; t++){
       if(i.item(t).type == "text" && i.item(t).readOnly == false){
          i.item(t).onfocus = function (){this.select()};
     }
 }
 }

This should work: 这应该工作:

<html>
<head>
</head>
<body>

<input type="text" id="theTextBox" onfocus="selectText()" value="some value"></input>
<script type="text/javascript">
function selectText() {
    document.getElementById("theTextBox").select();
};
</script>
</body>
</html> 

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

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