我可以触发浏览器在JS中聚焦元素吗



当我想关注某个元素时,我使用focus()函数,它从css中给了我:focus。问题是,当我专注于TAB键盘按钮时,我从css中获得了样式,但也从元素周围获得了一些蓝色样式,这可能是浏览器的焦点。我能用js以某种方式触发蓝色浏览器焦点吗?

您可以用focus-visibleCSS覆盖它。添加以下示例以供参考

button {
background: #F0FF00;
border: 1px solid #ccc;
font-size: 18px;
}
.clear-focus {
background: #00FF00;
}
.clear-focus:focus-visible, .clear-focus:focus  {
outline: none;
box-shadow: 0px 1px 6px 0px #000;
}
<input placeholder="click to focus here manually" type="text" />
<p>Now press tab</p>
<button >Button without focus override / default focuss css</button>

<p>Now press tab agian</p>
<button class="clear-focus">Override focuss css /  with custom css</button>

可见`CSS类。下面的示例代码:

我们可以使用outline: none;,它隐藏轮廓

:not(.focusable){
outline: none;
}
<input placeholder="You can't focus me*" type="text" />
<p>press tab</p>
<button class="focusable">You can focus me</button>
<button >You can't focus me*</button>
<p>again repeat</p>
<input placeholder="You can't focus me*" type="text" />
<p>press tab</p>
<button class="focusable">You can focus me</button>
<button >You can't focus me*</button>

<p>*Focus is possible to all element,but border is only hided,as per OP's question</p>

相关内容

  • 没有找到相关文章

最新更新