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小時內與您取得聯系。