lt;!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<input type="checkbox" id="checkbox1"><label for="checkbox1">库里</label><br>
<input type="checkbox" id="checkbox2"><label for="checkbox2">科比</label><br>
<input type="checkbox" id="checkbox3"><label for="checkbox3">麦迪</label><br>
<input type="checkbox" id="checkbox4"><label for="checkbox4">邓肯</label><br>
<input type="checkbox" id="checkbox5"><label for="checkbox5">奥尼尔</label><br><br>
<button>全选</button><button>全不选</button><button>反选</button>
</body>
</html>
<script type="text/javascript">
$(function(){
//匹配第一个button
$(':button:eq(0)').click(function(){
//全部选中 checked=true,在前台就是表示选中
$(':checkbox').attr('checked',true);
});
//匹配第二个button
$(':button:eq(1)').click(function(){
//全部取消 checked=false,在前台就是表示未选中
$(':checkbox').attr('checked',false);
});
//匹配第三个button
$(':button:eq(2)').click(function(){
//查找每一个复选框,然后取相反
$(':checkbox').each(function(){
$(this).attr('checked',!$(this).attr('checked'));
});
});
})
</script>
、CSS方法
.disabled { pointer-events: none; }
二、jQuery方法
方法一
$(this).click(function (event) {
event.preventDefault();
}
方法二
$('a').live('click', function(event) {
alert("抱歉,已停用!");
event.preventDefault();
});
注:此方法中的live亦可以为on, bind等方法
方法三
$('.disableCss').removeAttr('onclick'); //去掉标签中的onclick事件
通过removeAttr方法来控制html标签的属性已达到启用或禁用事件。另, 使用这种方式也可以控制其他事件或其他效果。
方法四
$('#button').attr('disabled',"true");//添加disabled属性
$('#button').removeAttr("disabled"); //移除disabled属性
注:和方法三是一样的, 不过disabled属性一般用在类型为button或submit的input上
原生js:
//页面
<div style="margin-top:20px;">
<button onclick="clickMethod()" id="btn">点我</button>
<button onclick="clearMethod()" >清除方法</button>
<button onclick="clearMethod2()" id="btn2">jq清除方法</button>
</div>
//原生js清除单击事件
function clickMethod(){
alert('我被点击了')
}
function clearMethod(){
// clickMethod="" //会报错
// clickMethod=function(){
// console.log("我被修改了")
// }
console.log(document.getElementById('btn'))
document.getElementById('btn').onclick="" //不会报错
}
jquery:直接了当
//jq清除单击事件
function clearMethod2(){
$('#btn').removeAttr('onclick')
}
vue
vue没有提供删除事件的方法,但是可以通过赋值的方式:
还有一种方式,通过增加一个变量的方式来完成不让元素的事件触发
*请认真填写需求信息,我们会在24小时内与您取得联系。