简体   繁体   English

Safari中的聚合物组件分派事件

[英]Polymer component dispatching event in Safari

I have a custom Polymer 2+ component like so: 我有一个自定义的Polymer 2+组件,如下所示:

<my-button id="my-button"></my-button>

Inside the component, I have this block of code in several places (on ready() , on a mouse press, etc.) for testing: 组件内部 ,我将此代码块放在多个位置(在ready() ,在鼠标按下等处)进行测试:

this.dispatchEvent(new Event('test'));
this.dispatchEvent(new CustomEvent('testCustom', {detail: {hey:'hey'}, bubbles:true, composed: true}));

In Chrome, both of these can be caught and logged in the below listener that implements the button: 在Chrome中, 这两种方法都可以捕获并记录在实现该按钮的以下侦听器中:

var myEl = document.getElementById('my-button')

myEl.addEventListener('test', function(){
   console.log('test reached');
})

myEl.addEventListener('testCustom', function(ev){
   console.log('test custom reached');
})

However, in Safari 11, on macOS Sierra, they are not. 但是,在Safari 11中,在macOS Sierra上则不是。 CustomEvent should be supported in Safari, am I doing something silly here? Safari应该支持CustomEvent ,我在这里做傻事吗?

Okay I figured it out, but the answer is bizarre. 好的,我知道了,但是答案很奇怪。 If I change this line: 如果我更改此行:

var myEl = document.getElementById('my-button')

to

var myEl = document.querySelector('my-button')

it will all of a sudden work in Safari. 它将在Safari中突然起作用。 I have no explanation as to why, there shouldn't be a naming conflict as getElementById() should still be able to select the element. 我没有任何解释,为什么不应该有命名冲突,因为getElementById()应该仍然能够选择元素。

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

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