Javascript 如何从提交按钮上移除焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2439565/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:20:30  来源:igfitidea点击:

How to remove focus from submit button

javascripthtmlcss

提问by ant

How can I remove focus from submit button? I don't know why it is there in the first plate, and it looks really ugly here is a screenshot :

如何从提交按钮上移除焦点?我不知道为什么它在第一个盘子里,它看起来真的很难看,这是一个截图:

alt text

替代文字

And here is how it should look like, it regains its appearance after I click beside it and hover it.alt text

这是它的外观,在我单击它旁边并将其悬停后,它会恢复外观。替代文字

回答by ant

This worked :

这有效:

var selectedInput = null;
$(document).ready(function() {
    $('input, textarea, select').focus(function() {
        this.blur();
    });
});

回答by Brian Moeskau

Try this in your button's CSS, or that of the enclosing <a>if that's what you are using in the markup (reference):

在您的按钮的 CSS 中尝试这个,或者<a>如果这是您在标记中使用的(参考),则在封闭的 CSS 中尝试:

-moz-outline:0 none;
outline:0 none;

Bear in mind that it is a valuable accessibility/keyboard feature, so ideally you would provide some alternate focus hint that is more visually pleasing to you.

请记住,这是一项有价值的辅助功能/键盘功能,因此理想情况下,您可以提供一些在视觉上更令您满意的替代焦点提示。

回答by casraf

In your body or form...

在你的身体或形式...

<form onready="document.getElementById('submit').blur();">
...
<input type="submit" id="submit" />
</form>

回答by naivists

The first submit button in a form is always "in focus", hence, when you hit enter, your form gets submitted.

表单中的第一个提交按钮始终处于“焦点”状态,因此,当您按 Enter 键时,您的表单将被提交。

See responses to a similar post: Stopping IE from highlighting the first submit-button in a form

查看对类似帖子的回复:Stopping IE from highlighting the first submit-button in a form

回答by cbmckay

You can code your button as:

您可以将按钮编码为:

<button onClick='this.form.submit()'>Button Text</button>

That removes the "submit" functionality to code and thus the focus problem.

这消除了代码的“提交”功能,从而消除了焦点问题。