简体   繁体   English

jQuery ON事件未在IE8中触发

[英]jquery ON event not firing in IE8

I made this jsfiddle to demonstrate what I am meaning, but unfortunately JSfiddle itself doesn't seem to work with IE8 so you need to test this jsfiddle code in a stand alone page: http://jsfiddle.net/4Bdbn/ 我制作了这个jsfiddle来演示我的意思,但是不幸的是JSfiddle本身似乎无法与IE8配合使用,因此您需要在独立的页面中测试此jsfiddle代码: http : //jsfiddle.net/4Bdbn/

With IE8 the above ON events does not fire, absolutely nothing happens. 使用IE8,上述ON事件不会触发,绝对不会发生任何事情。 even adding an alert("hi") to the function does nothing; 甚至向该函数添加一个alert(“ hi”)都无济于事; it doesn't get called, plus no errors are reporting in the console. 它不会被调用,而且控制台中没有错误报告。

On a side note, is e.preventDefault() necessary to prevent a function being executed multiple times when you have multiple events triggering the same function, such as .on("touchstart click",.... ? In all situations? 附带说明一下,当您有多个事件触发同一功能时,例如.on("touchstart click",.... ),是否需要e.preventDefault()来防止该功能被多次执行?

jQuery version 1.8.3 so I believe IE8 is a supported browser. jQuery 1.8.3版,因此我相信IE8是受支持的浏览器。

ps. PS。 Im using IE10 in Browser Mode IE8. 我在浏览器模式IE8中使用IE10。

EDIT: My simple test page which is not working in IE8 (for me): http://www.personaltrainer.com.au/test.php 编辑:我的简单测试页无法在IE8中使用(对我来说): http : //www.personaltrainer.com.au/test.php

The relevant code section is... 相关代码部分为...

<script type="application/javascript">
$(document).ready(function () {
    $("body").on("click touchstart",".something",function(e) {
        $(this).text($(this).text() + " "+e.type);
        e.preventDefault();
    });
});
</script>

Thanks! 谢谢!

You're using an invalid type attribute ( application/javascript ) on your script tag. 您在脚本标签上使用了无效的type属性( application/javascript )。 Change it to text/javascript or simply remove it all together. 将其更改为text/javascript或将其全部删除。

This works just fine in IE8 (real version) 这在IE8(实际版本)中可以正常工作

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
<script>
$(document).ready(function () {
    $("body").on("click touchstart",".something",function(e) {
        $(this).text($(this).text() + " "+e.type);
    });
});
</script>

maybe you should remove the touchstart event first to check if it works! 也许您应该先删除touchstart事件以检查其是否有效! IE8 actually dosn't support touch action! IE8实际上不支持触摸操作!

$(function () {
    $("body").on("click",".something", function(e) {
        $(this).text($(this).text() + " " + e.type);    
        //e.preventDefault();   
    });
});

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

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