1、prop 和 attr
对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
我们自己自定义的DOM属性,在处理时,使用attr方法。
结果返回true / false
2、jQuery如何获取radio的值
1.获取选中值,三种方法都可以:
$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();
2.设置第一个Radio为选中值:
$('input:radio:first').attr('checked','checked');
或者
$('input:radio:first').attr('checked', 'true');
注:attr("checked",'checked')= attr("checked",'true')= attr("checked", true)
3.设置最后一个Radio为选中值:
$('input:radio:last').attr('checked', 'checked');
或者
$('input:radio:last').attr('checked', 'true');
4.根据索引值设置任意一个radio为选中值:
$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....
或者
$('input:radio').slice(1,2).attr('checked', 'true');
5.根据Value值设置Radio为选中值
$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');
或者
$("input[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');
3、JSON.parse()和JSON.stringify()
parse用于从一个字符串中解析出json对象,如
var str ='{"name":"huangxiaojian","age":"23"}'
结果:
JSON.parse(str)
Object
. age: "23"
. name: "huangxiaojian"
. __proto__: Object
注意:单引号写在{}外,每个属性名都必须用双引号,否则会抛出异常。
stringify()用于从一个对象解析出字符串,如
var a = {a:1,b:2}
结果:
JSON.stringify(a)
"{"a":1,"b":2}"