高版本jquery的prop和attr使用区别的小例子

<script src="jquery.min.js"></script>
<script>

function test()
{
$("input").each(function(){

console.log($(this).prop("checked"));

});
}
</script>

<input type="radio" action="hello" name="test" onmouseover="test()">
<input type="radio" action="hello2" name="test" checked="checked">

<? exit();
//prop action 属性undefined
//attr action 属性正确读取 可以得出attr属性用于读取自定义属性

//attr checked 属性 一开始未选中无论是否后来选中都是undefined, 但是假如是一开始就选中的话,就能正确读取,但是变化后无法读取变化的值
//prop 能正确读取, 选中返回true,未选中返回false.
//由此可以看出当为自定义属性的时候,选取属性应该用attr方法, 固有属性应该用prop方法来获取

?>

猜你喜欢

转载自www.cnblogs.com/tpwBlog/p/9074275.html
今日推荐