jquery 版本兼容性问题集合


使用jquery的不少时候代码仍是停留在过去1.2.6,1.4.2这些版本的API用法上面,其实以后的版本修改了不少,因此不当心会碰到不少坑,这里不停的更新列举下问题:javascript

先看一段常规的1.4.2版本的代码:html

$("#categoryAndItems div").delegate("input[type='checkbox']:eq(0)", 'click', function(){
			if ($(this).attr('checked')) {
			    console.log('c=' + $(this).prop('checked'));
				$(this).parent().parent().find("input[type='checkbox']:gt(0)").attr('checked', true);
			} else {
				$(this).parent().nextAll().find("input[type='checkbox']").attr('checked', false);
			}
		});

会发现这段代码在1.10这个版本中出现不少问题,首先attr这个属性在高版本的jquery中获取选择的状态,就会有各类问题;

其次在使用attr变为某个值以后,好比attr('checked', true);去看下html的源码,会发现checkbox的源码中多了checked="checked"java

即便使用.attr('checked', false); 也没法修改其属性;jquery

因此最后修改成:this

$("#categoryAndItems div").on('click', "input[type='checkbox']:eq(0)", function(){
			    console.log('c=' + $(this).prop('checked'));
            if ($(this).prop('checked')) {
			    console.log('check=');
				$(this).parent().parent().find("input[type='checkbox']:gt(0)").prop('checked', true);
			} else {
                $(this).parent().parent().find("input[type='checkbox']:gt(0)").prop('checked', false);
			}
		});