addEventListener在Firefox中工作,但不在Chrome中 [英] addEventListener working in firefox but not in chrome

查看:135
本文介绍了addEventListener在Firefox中工作,但不在Chrome中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的代码在Mozilla Firefox 31.0中可用,但在Google Chrome中不可用38.0.2125.104
如何解决此问题?

The given code works in Mozilla Firefox 31.0 but not in Google Chrome 38.0.2125.104 How can this issue be solved?

请注意: change()函数或jQuery在这里不是一个选项!

Please Note : Using the change() function or jQuery is not an option here!

<body>
    <select id="category">
        <option value="movies">Movies</option>
        <option value="artist">Artists</option>
        <option value="authors">Authors</option>
        <option value="chemistry">Chemistry</option>
    </select>
</body>

<script>
window.onload = initialise;
function initialise() {
    var options = document.getElementsByTagName('option');
    for (var i = 0; i < options.length; i++) {
        options[i].addEventListener("click", getQuestion, false);
        //console.log(i);
    }
}

function getQuestion() {
    console.log("getting question..");
}

</script>
</html>


推荐答案

我不知道为什么他们都不是绑定,但你不是寻找on change函数?所以你可以绑定到那个一次?

I don't know why they all aren't binding but aren't you look for the on change function? So you can just bind to that once?

//Untested
var Category = document.getElementsById('category');
Category.addEventListener("change", getQuestion, false);

此外,如果你不反对Jquery,你可能想看看它经常摆脱的跨浏览器问题。在Jquery中:

Also, If you're not against Jquery, you might want to look into it it often gets rid of cross-browser issues. In Jquery:

 $('body').on('change', '#category', function() {
     getQuestion($(this));
 })

这篇关于addEventListener在Firefox中工作,但不在Chrome中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆