1 回答

TA貢獻1998條經驗 獲得超6個贊
我認為原因出在事件捕獲。
先說解決方案:在你的代碼里,為click
綁定的函數添加return false
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<label class="man"><input type="radio" name="sex" id="man">man</label>
<label class="woman"><input type="radio" name="sex" id="woman">woman</label>
<script src="JS/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function(){
var score = 0;
$("#man").click(function(){
score = score + 1;
console.log(score);
// score = 0;
/*修改部分開始*/
return false;
/*修改部分結束*/
});
$("#woman").click(function(){
score = score + 2;
console.log(score);
// score = 0;
/*修改部分開始*/
return false;
/*修改部分結束*/
});
})
</script>
</body>
</html>
現在我們來驗證一下猜測:假設是事件捕獲,那么只需要在觸發的時候彈出觸發事件的對象即可。所以我們修改一下click
綁定的函數:
$("#man").click(function(event){
score = score + 1;
console.log(score);
// score = 0;
/*修改部分開始*/
console.log(event.target)
/*修改部分結束*/
});
看下結果:
//控制臺打印的結果
1
<label class="man">…</label>
2
<input type="radio" name="sex" id="man">
添加回答
舉報