简体   繁体   English

Chrome和iPad带有querySelectorAll的问题

[英]Issue with chrome and iPad with querySelectorAll

I have the following code: 我有以下代码:

var showonlyswitch = document.querySelectorAll('#showonlyswitch > optgroup > option');
for (var y = 0; y<showonlyswitch.length; y++) {
     showonlyswitch[y].addEventListener('click', showonlyproducts);
}

working in firefox, but not in chrome and not in iOS safari. 在firefox中工作,但不在chrome中,不能在iOS野生动物园中工作。

As far as I know (and "caniuse" tells me), querySelectorAll and addEventListener should both be supported by both browsers. 据我所知(和“ caniuse”告诉我),两个浏览器均应支持querySelectorAll和addEventListener。 (iOS safari v.8.1 and chrome v.39.0.2171.95 (64-bit)) (iOS Safari v.8.1和Chrome v.39.0.2171.95(64位))

The function I am calling with this is quite simple: 我用这个调用的函数非常简单:

function showonlyproducts() {

    var searchvalue = document.getElementById('showonlyswitch').value;
    if (searchvalue == 1) {
        window.location.href = 'pricing?close=1';
    }
}

First I thought window.location.href would cause some problems, but I tried to console.log some gibberish and it worked in firefox but not in chrome. 首先,我认为window.location.href会引起一些问题,但是我尝试在console.log一些乱码,它在Firefox中有效,但在chrome中不起作用。

I also tried to change 我也尝试改变

var showonlyswitch = document.querySelectorAll('#showonlyswitch > optgroup > option');

to

var showonlyswitch = document.querySelectorAll('#showonlyswitch > option');

but that (obviously) doesn't work in any browser. 但这(显然)在任何浏览器中均不起作用。

The select is separated in some optgroups and the function should trigger on click on an option, as the change event isn't sufficient for the situation because it is possible that a solution is chosen without the change event being triggered (no change). 选择被分离在某些optgroup中,并且该功能应在单击某个选项时触发,因为更改事件不足以解决这种情况,因为选择解决方案时可能不会触发更改事件(无更改)。

As I said, the code is working perfectly fine in firefox, so there shouldn't be any typos or something (usually ...), there are no errors in chrome's console. 就像我说的那样,代码在Firefox中工作得很好,因此不应有任何错别字或其他内容(通常是...),Chrome的控制台中没有错误。

Are there any known issues which come to your mind with anything here? 这里有什么让您想到的已知问题吗?

Binding click events to option elements is not reliable. 将点击事件绑定到option元素是不可靠的。 Instead use onchange event: 而是使用onchange事件:

var showonlyswitch = document.querySelector('#showonlyswitch');
showonlyswitch.addEventListener('change', showonlyproducts);

As for this part 至于这部分

the change event isn't sufficient for the situation because it is possible that a solution is chosen without the change event being triggered 更改事件不足以适应这种情况,因为有可能在不触发更改事件的情况下选择了解决方案

Try tom bind click event on #showonlyswitch select element itself rather then option elements. 尝试在#showonlyswitch选择元素本身而不是option元素上#showonlyswitch tom bind click事件。

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

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