attr和prop的区别
1、对于HTML元素本身就带有的固有属性,在处理时,使用prop方法
2、对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
3、checkbox,radio和select中对应checked和selected属于固有属性, 因此需要用prop方法,用attr会出现undefined
4、action="delete",delete是自己定义的属性,用attr方法
1 let vote = []; 2 /* 3 attr和prop的区别 4 1、对于HTML元素本身就带有的固有属性,在处理时,使用prop方法 5 2、对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。 6 3、checkbox,radio和select中对应checked和selected属于固有属性,因此需要用prop方法,用attr会出现undefined 7 4、action="delete",delete是自己定义的属性,用attr方法 8 */ 9 $('input:checkbox[name=hobby]').each(function () { 10 // console.log($(this).attr('checked',true)); 11 //设置true为全部选中 12 if ($(this).prop('checked')) { 13 vote.push($(this).val()) 14 } 15 }); 16 //join(),不加参数默认",",加了参数以参数作为分隔符,toString也可以 17 return vote.join();