<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.hidden{
display:none;
</style>
</head>
<body>
<input id="a" type="button" value="a"/>
<input id="b" type="button" value="b" />
<div id='i1' class="hidden" style="margin:0 auto;width:300px;height:200px;background-color:grey">
<input id='i2' type="button" value="确认" />
<input type="button" value="取消" οnclick="Cancel()"/>
</div>
<script src="jquery-1.12.4.js"></script>
<script>
$('#a').on('click',function(){
$('#i1').removeClass('hidden');
$('#i2').off().on('click',function(){
console.log('a');
})
})
$('#b').on('click',function(){
$('#i1').removeClass('hidden');
$('#i2').off().on('click',function(){
console.log('b');
})
})
// 下面这种方式不行,如果你点击了取消,下次再点击b,再点确认就会输出2次b。而且这样,即使a中写了off(),但是如果点击a
//在点击取消,再点击b,确认,会输出a和b,所以要a , b 同时写off
$('#b').on('click',function() {
$('#i1').removeClass('hidden');
$('#i2').click(function () {
console.log('b');
})
})
function Cancel(){
$('#i1').addClass('hidden');
}
</script>
</body>
</html>