简体   繁体   English

event.target.blur()不起作用

[英]event.target.blur() not working

I have a series of Bootstrap buttons like this: 我有一系列这样的Bootstrap按钮:

<button :disabled="page === lastPage" type="button" class="btn btn-default" @click="getPage(lastPage)">
    <span class="glyphicon glyphicon-forward"></span>
</button>

But the first line of the getPage method does not work: 但是getPage方法的第一行不起作用:

event.target.blur();

Which is very strange, because I have another button in another component, on which event.taget.blur() works perfectly: 这很奇怪,因为我在另一个组件中有另一个按钮,event.taget.blur()完美地工作:

<button class="btn btn-default pull-right" @click="show()" type="button">Batch
    <span :class="{'glyphicon glyphicon-chevron-down': showForm, 'glyphicon glyphicon-chevron-up': !showForm}"></span>
</button>

Does anyone know why this might be? 有谁知道为什么会这样?

EDIT: I think it's when I click inside the SPAN that the blur doesn't work. 编辑:我认为当我点击SPAN内部时,模糊不起作用。

EDIT: oh well I solved it - I also need event.target.parentNode.blur() as well. 编辑:哦,我解决了它 - 我也需要event.target.parentNode.blur()

You likely want to use 你可能想用

event.currentTarget.blur()

That will always be the element you attached the event listener to where as event.target is the element the event originated from. 这将始终是您将事件侦听器附加到的元素,因为event.target是事件源自的元素。

In this case, because the button contains a span, sometimes the user was clicking on the button itself, and sometimes on the span within. 在这种情况下,因为按钮包含跨度,有时用户单击按钮本身,有时在内部跨度。 Hence, to reliably blur the button I needed: 因此,要可靠地模糊我需要的按钮:

event.target.blur();
event.target.parentNode.blur();

I would consider using 我会考虑使用

if (document.activeElement != document.body) {
    document.activeElement.blur()
}

rather than navigating through nodes as it is less prone to error if the mark up changes. 而不是在节点中导航,因为如果标记改变则不易出错。

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

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