jQuery事件侦听器在IE 7/8中不起作用 [英] jQuery event listener doesn't work in IE 7/8
问题描述
问题有点棘手.我的代码无法动态加载表中的表行,然后将事件附加到所有表"单元格,但在IE7/8中不起作用.但是有时它确实没有明显的原因.
the problem is a bit tricky. My code to dynamically load table rows inside a table and then attach events to all the Table cells doesn't work in IE7/8. But sometimes it does work for no apparent reason.
这里有一个小样本: http://jsbin.com/ivarus/6/edit
Here is a little sample: http://jsbin.com/ivarus/6/edit
这是我在应用程序中所做的简化版本
This is a simplified version of what I'm doing in my app
以下是进行复制的确切步骤: 1)打开IE并从开发工具中将其设置为IE7/8(如果您已经在运行IE8,则无需这样做) 2)导航到 http://jsbin.com/ivarus/6/edit 预期:与TD相关的点击在输出"部分中起作用 实际上:只有在按下运行脚本"后,事件监听器才能开始工作
Here are the exact steps to repro: 1) Open IE and set to IE7/8 from dev tools (not necessary if you're already running IE8) 2) Navigate to http://jsbin.com/ivarus/6/edit Expected: clicks attached to TD work in the Output section Actually: the event listeners start working only after pressing "Run script"
在所有其他浏览器(IE9 +,Chrome,FF)中,此操作立即生效.为什么?我做错什么了吗?我应该寻找一种替代方法吗?
In all other browsers (IE9+,Chrome,FF) this works immediately. Why? Am I doing something wrong ?Should I look for an alternative approach ?
推荐答案
在脚本运行之前,您需要等待文档就绪事件.像这样封装您的代码.
You need to wait for the document ready event before your script runs. Enclose your code like this.
<script>
$(function ()
{
//your code here
});
</script>
这篇关于jQuery事件侦听器在IE 7/8中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!