鼠标点击文字选中复选框

在前台,我们经常用复到选框,来选择多个感兴趣的对象,但只有单击复选框才可选中。现用jQuery写了一小段代码,实现鼠标点击文字选中复选框。
<html>
<head>
<title>鼠标点击文字选中复选框</title>
<script language="javascript" type="text/javascript" src="jQuery1.3.2/jquery-1.3.2.min.js"></script>
<!-- 当鼠标在文字之上显示为手形 -->
<style type="text/css">
.hand{ cursor : hand ;}
</style>
</head>
<body>
<div><input type="checkbox" value="film" name="hobby" checked="checked"><span>film</span></div>
<div><input type="checkbox" name="hobby"><span>game</span></div>
<div><input type="checkbox" name="hobby"><span>books</span></div>
<script language="javascript" type="text/javascript">
$("span").bind("mouseover",function(){$(this).addClass("hand");}).bind("click",function(){
// 由span标签获取input标签 var obj=$(this).parent().children(0);
if(obj.attr("checked")==true){ obj.removeAttr("checked"); }
else { obj.attr("checked","checked"); }
});
</script>
</body>
</html>
功能虽然实现了,但还有点不满意,就是标签的嵌套,加上div才实现这个功能。不知大家有何好的idea,望不吝赐教啊!
原文地址:https://www.cnblogs.com/qingliuyu/p/1789679.html