Dal*_*len 4 jquery compatibility version prop
我试图通过以下方式测试.prop()当前jQuery中是否存在该方法(出于兼容性原因):
if(typeof $.prop === 'function')
Run Code Online (Sandbox Code Playgroud)
我希望的是,上述条件是true用于jQuery >= 1.6和false对jQuery < 1.6我可以从理解文档
无论如何,在jsfiddle上测试这个,导致:
typeof $.prop === 'function' 是:
true 什么时候 jQuery >= 1.6false 什么时候 jQuery < 1.6 and jQuery > 1.3true 什么时候 jQuery <= 1.3这是一个非常非常简单的脚本,提供上面的结果(只需切换jQuery版本,看看我所描述的).
当我尝试使用.prop()jQuery即1.3时,我得到了.prop is not a function错误.同样的问题也会在jsfiddle之外进行测试.这种行为是正常的吗?我怎么能真正测试是否.prop()可用?
谢谢
alert(typeof $.fn.prop === 'function')
您想检查.prop生活在jQuery原型上的方法$.fn.这在1.3中是错误的.
此外,我会避免jQuery版本的功能检测,而是支持特定版本(和更高版本).
小智 6
我所做的是为jQuery版本构建一个兼容的函数,它不使用prop:
(function($){
if (typeof $.fn.prop !== 'function')
$.fn.prop = function(name, value){
if (typeof value === 'undefined') {
return this.attr(name);
} else {
return this.attr(name, value);
}
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
您可以测试以下代码:http://jsfiddle.net/JtK2Q