ctrl+鼠标左键监听

时间:2023-03-09 15:50:25
ctrl+鼠标左键监听
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>div多选</title>
<style>
.container{
width: 100px;
height: 100px;
background-color: #E6E6E6;
border:1px solid #cccccc;
text-align: center;
line-height: 100px;
float: left;
margin-left: 20px;
}
.redBorder{
border: 1px solid red;
}
</style>
<script src="js/jquery-3.4.1.js" type="text/javascript"></script>
</head>
<body>
<div class="box">
<div class="container">DIV</div>
<div class="container">DIV</div>
<div class="container">DIV</div>
</div>
</body>
<script>
$(".container").mousedown(function(event) {
if (event.button == 0) { // 鼠标左键
if (event.ctrlKey) { // ctrl
if ($(this).hasClass("redBorder")) {
$(this).removeClass("redBorder");
} else {
$(this).addClass("redBorder");
}
}
}
})
</script>
</html>