Javascript 文本框焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2191749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
textbox focus out
提问by Santhosh
I need to focus out from the textbox when it focus in.
当它聚焦时,我需要从文本框中聚焦。
I try to set focus for outer div and its working fine in IE but not in mozilla.
我尝试为外部 div 设置焦点,并且它在 IE 中工作正常,但在 mozilla 中却没有。
How do I do this?
我该怎么做呢?
This is my current code:
这是我当前的代码:
<div id="outer"> <input type = "textbox" /></div> Onfocus: document.getElementById("outer").focus()
采纳答案by Santhosh
I have tried all the answers and not worked in all the browsers. And I combined all together in to it.
我已经尝试了所有答案,但并未在所有浏览器中工作。我把所有的东西都结合在一起。
TextBox.readonly = true;
TextBox.readonly = true;
OnFocus:
焦点:
var curText = TextBox.value;TextBox.value = "";TextBox.value = curText;TextBox.blur();TextBox_Parent.focus()
var curText = TextBox.value;TextBox.value = "";TextBox.value = curText;TextBox.blur();TextBox_Parent.focus()
And its working fine in all the browsers
它在所有浏览器中都能正常工作
回答by Darin Dimitrov
I wonder what's the purpose of using a textbox in this case if the user can never write anything inside. Just add a disabled="disabled"attribute or readonly="readonly"(in case you want to post the value).
如果用户永远不能在里面写任何东西,我想知道在这种情况下使用文本框的目的是什么。只需添加一个disabled="disabled"属性或readonly="readonly"(如果您想发布该值)。
回答by Andy E
In HTML:
在 HTML 中:
<input type="text" onfocus="this.blur();" />
In JS:
在JS中:
document.getElementById("input1").onfocus = function () { this.blur(); }
Some elements cannot accept focus without being editable.
某些元素在不可编辑的情况下无法接受焦点。
回答by Felix Kling
Where is the point in that? JS would be (didn't test it):
这其中的重点在哪里?JS 将是(没有测试它):
$('#textbox').focusin(function() {
$(this).focusout();
});
回答by vette982
/*for textarea*/
$(document).ready(function() {
$('textarea[type="text"]').addClass("idleField");
$('textarea[type="text"]').focus(function() {
$(this).removeClass("idleField").addClass("focusField");
if (this.value == this.defaultValue){
this.value = '';
}
if(this.value != this.defaultValue){
this.select();
}
});
$('textarea[type="text"]').blur(function() {
$(this).removeClass("focusField").addClass("idleField");
if ($.trim(this.value == '')){
this.value = (this.defaultValue ? this.defaultValue : '');
}
});
});
});
that's what I used on my form.
这就是我在表单上使用的内容。

