最近在学习jQuery(版本jquery-1.9.1.js),要求用jQuery实现全选/全不选、反选,在IE(IE8)中没有问题,但在火狐浏览器中调试的时候出现了一些小问题,达不到效果。
HTML代码如下:
<input name="intrest" type="checkbox" />足球 <input name="intrest" type="checkbox" />篮球 <input name="intrest" type="checkbox" />羽毛球 <input name="intrest" type="checkbox" />乒乓球<br/> <button id="allbtn">全选</button> <button id="notallbtn">全不选</button> <button id="reversebtn">反选</button> <button>提交</button>
@H_301_18@
jQuery代码:
<div class="jb51code"> <pre class="brush:js;"> <script type="text/javascript" src="jquery-1.9.1.js"> <script type="text/javascript"> $().ready(function(){ //全选/全不选复选框 $("#selectal1").click( function(){ if($(this).attr("checked")==true){ $("input:checkBox[id!='selectal1']").each(function() { $(this).attr("checked",true); }); }else{ $("input:checkBox[id!='selectal1']").each(function() { $(this).attr("checked",false); }); } }); //全选按钮 $("#allbtn").click(function(){ $("input:checkBox[id!='selectal1']").each(function() { $(this).attr("checked",true); }); }); //全不选按钮 $("#notallbtn").click(function(){ $("input:checkBox[id!='selectal1']").each(function() { $(this).attr("checked",false); }); }); //反选按钮 $("#reversebtn").click(function(){ $("input:checkBox[id!='selectal1']").each(function() { $(this).attr("checked",!$(this).attr("checked")); }); }); })
查了下API prop属性是这样的:
概述
获取在匹配的元素集中的第一个元素的属性值。 随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误。jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误。 jQuery API明确说明,1.6+的jQuery要用prop,尤其是checkBox的checked的属性的判断,于是乎把js代码里面的attr换成prop就行了。
代码:
希望对大家有所帮助。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。