简体   繁体   English

IE中不会触发模糊事件-Angular 2+

[英]Blur event is not triggering in IE - Angular 2+

The below problem is related to Blur not working - Angular 2 thread. 以下问题与模糊不起作用-Angular 2线程有关。

I have a shared component of custom select and I am trying to add a blur event to close when the component is not in focus. 我有一个自定义选择的共享组件,并且我试图添加一个模糊事件以在该组件不清晰时关闭。

// HTML

<div (blur)="closeDropDown()" tabindex="0">
  <span (click)="selectClicked()" class="placeholder">
    Select Item 
  </span>
  <div class="options">
    <ul>
       <li *ngFor="let item of data"> 
       <span>{{item}}</span></li>
    </ul>
  </div>

//TS

 selectClicked() {
   this.dropdownOpen = true;
 }

 closeDropDown() {
   this.dropdownOpen = false;
 }

I was able to implement the blur event by adding the tabindex mentioned in the thread(works in all browsers except IE). 我能够通过添加线程中提到的tabindex来实现模糊事件(在除IE之外的所有浏览器中均可)。 But blur event is not firing in IE(version > 10). 但是模糊事件在IE(版本> 10)中不会触发。

I tried to use focusout instead of blur but the selected value is not getting attached to the custom select and requires many selections to select an option. 我尝试使用聚焦而不是模糊,但是所选值未附加到自定义选择中,并且需要很多选择才能选择选项。

Why does blur is not triggering on div and are there any alternatives that I can use on block level elements? 为什么模糊不会在div上触发,并且我可以在块级元素上使用其他替代方法吗?

I was able to fix the problem by adding focusout and tabindex=-1 to a div element. 我可以通过将focusout和tabindex = -1添加到div元素来解决此问题。 But by doing this value was not getting set to my custom dropdown. 但是通过执行此值并没有将其设置为我的自定义下拉菜单。

The reason for that is when I used focusout, the event was bubbling back to parent and took more time to set the dropdown value after selection. 这样做的原因是当我使用焦点输出时,该事件冒泡回到了父级,并且在选择之后花费了更多时间来设置下拉值。

So I missed to stop the bubbling event, the fix was to stop the propogation. 所以我错过了停止冒泡事件的机会,解决方法是停止传播。

// html
<div (focusout)="closeDropDown($event)" tabindex="-1"></div>

// ts
closeDropDown(event) {
  event.stopPropogation();
  this.dropdownOpen = false;
}

The focusout event fires when an element is about to lose focus. 当元素即将失去焦点时,触发聚焦事件。 The main difference between this event and blur is that focusout bubbles while blur does not. 此事件和模糊之间的主要区别在于,焦点模糊会冒泡,而模糊不会。

Find more about this on blur vs focusout -- any real differences? 模糊vs聚焦上找到更多有关此的信息-真正的区别是什么? and https://developer.mozilla.org/en-US/docs/Web/API/Element/focusout_event https://developer.mozilla.org/zh-CN/docs/Web/API/Element/focusout_event

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

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