IE 9 jQuery错误:“对象不支持...'打开'"? [英] IE 9 jQuery error: "Object doesn't support ... 'on'"?
问题描述
我需要帮助.我在IE9中收到以下错误.该代码正在FireFox中运行:
I need help. I'm getting the following error in IE9. The code is working in FireFox:
SCRIPT438: Object doesn't support property or method 'on'
这是在以下代码上提出的:
This is raised on the following code:
$(function() {
$(document).on('change', '#dropdownval select', function(event) {
//logic function
});
});
推荐答案
在Firefox中可以运行,而不是IE吗?
我将确保两个浏览器都加载相同的源.老实说,由于Firefox不存在,因此在使用jQuery 1.6时使用.on
有点不可能.
在控制台中(IE中的F12开发人员工具和Firefox中的Firebug)键入以下内容:
Within your console (F12 Developer Tools in IE and Firebug in Firefox) type the following:
jQuery.fn.jquery
这应该返回在该特定页面上加载的jQuery的当前版本.例如,从今天开始,在StackOverflow上在此处运行该命令将导致"1.7.1"
.其次,要测试.on
方法是否存在,您可以在不带括号的情况下访问它,使用双重否定将其强制转换为真正的布尔值:
This should resturn the current version of jQuery loaded on that particular page. For instance, as of today, running that command here on StackOverflow results in "1.7.1"
. Secondly, to test for the presence of the .on
method, you can access it without its parenthesis, using double-negation to cast it to a true boolean:
!!jQuery.fn.on // True if .on is present, False otherwise
对于jQuery 1.6,请使用.delegate而不是.on
由于使用的是jQuery 1.6,因此无法访问 .on
方法,是jQuery 1.7中引入的.适当的后备方法将改为使用 .delegate
方法,或者您可以升级到最新版本jQuery( Microsoft CDN , jQuery CDN ).
.delegate
的语法遵循
The syntax for .delegate
follows that of .on
pretty closely:
$("#dropdownval").delegate("select", "change", function(event){
alert( $(this).val() );
});
提琴: http://jsfiddle.net/jonathansampson/rMBn4/
您已经使用 .on
代码关闭了,但是您不想绑定事件直到document
对象-这意味着该事件需要传播可能很长的距离,然后才能进行处理.相反,就像在 .delegate
示例中所做的那样,我们将绑定到更接近select的内容.元素:
You were close with your .on
code, but you don't want to bind the event as far down as the document
object - this would mean the event would need to propagate a potentially long distance before being handled. Instead, as we did with the .delegate
example, we'll bind to something closer to the select element:
$("#dropdownval").on("change", "select", function(event){
alert( $(this).val() );
});
提琴: http://jsfiddle.net/jonathansampson/rMBn4/1/
您会注意到,两者之间的主要区别在于链接方法的前两个参数的顺序.使用 .delegate
,选择器将在事件之前.使用 .on
,顺序相反.
You'll note that the main difference between the two is the order of the first two parameters to the chained method. With .delegate
, the selector precedes the event. With .on
, the order is reversed.
这篇关于IE 9 jQuery错误:“对象不支持...'打开'"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!