<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<SCRIPT LANGUAGE="JavaScript" src="./jquery.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$(".a").hover(
function (){
$(this).css({background:"#e9e9e9"});
},
function (){
$(this).removeAttr("style");
}
);
$(".a").click(function (){
var id=$(this).attr('id');
if($("#"+id+" > input").attr("checked")==true){
$("#"+id+" > input").attr("checked",false);
}else{
$("#"+id+" > input").attr("checked",true);
}
});
});
</script>
</head>
<body>
<div id="1" class="a">
选择本项<input type="checkbox" name="checkbox" value="1">
</div>
<div id="2" class="a">
选择本项<input type="checkbox" name="checkbox" value="2">
</div>
<div id="3" class="a">
选择本项<input type="checkbox" name="checkbox" value="3">
</div>
<div id="4" class="a">
选择本项<input type="checkbox" name="checkbox" value="4">
</div>
</body>
</html>问题:
<input type="checkbox">单击无效果 如何解决?
单击div"a"的效果保留
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb232" />
<SCRIPT LANGUAGE="JavaScript" src="./jquery.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$(".a").hover(
function (){
$(this).css({background:"#e9e9e9"});
},
function (){
$(this).removeAttr("style");
}
);
$(".a").click(function (){
var id=$(this).attr('id');
$("input:checkbox").attr("checked",false);
$("#"+id+" > input").attr("checked",true);
});
});
</script>
</head>
<body>
<div id="1" class="a">
选择本项<input type="checkbox" name="checkbox" value="1">
</div>
<div id="2" class="a">
选择本项<input type="checkbox" name="checkbox" value="2">
</div>
<div id="3" class="a">
选择本项<input type="checkbox" name="checkbox" value="3">
</div>
<div id="4" class="a">
选择本项<input type="checkbox" name="checkbox" value="4">
</div>
</body>
</html>
<div class="a" style="background:#eee">
<div style="border:1px solid #ddd;width:150px;float:left">内容一</div>
<div style="border:1px solid #ddd;width:150px;float:left">内容二</div>
<div style="border:1px solid #ddd;width:150px;float:left">内容三</div>
<div style="border:1px solid #ddd;width:150px;float:left">内容四</div>
<div style="border:1px solid #ddd;width:150px;float:left">内容五</div>
<div style="border:1px solid #ddd;width:150px;float:left"><input type="checkbox" name="checkbox" value="4"></div>
<div style="clear:both"></div>
</div>抱歉没有写明白
想实现上面的结构 在单击class="a"的时候本行内的<input type="checkbox">将被选中
效果跟邮箱里的右键列表一样!
不知如何解决?
你点了 <input type="checkbox"> 之后外面的DIV的click事件同时被执行了 这样就产生了抵消的效果
所以点击了 <input type="checkbox"> 就要不执行外面DIV的CLICK
$(".a").click(function (event){
var id=$(this).attr('id');
$("#"+id+" > input").each(
function(){
if(event.target!=this){
this.checked=!this.checked
}
}
)可能写的不对 不过就是这个思路 DIV的ONCLICK要判断 触发CLICK事件的源对象是不是 input.check
var id=$(this).attr('id');
if(event.target.tagName!="INPUTT"){
alert("1");
if($("#"+id+" > input").attr("checked")==true){
$("#"+id+" > input").attr("checked",false);
}else{
$("#"+id+" > input").attr("checked",true);
}
}
});
添加红色部分后FF错误控制台提示 预期为选择器 请问如何解决?
因以前未应用过 请高手帮忙解决.
有无其他更好方法解决效果?