如何使用 jQuery 多事件觸發同一個功能?
要使用多個事件觸發相同的功能,請在 on() 方法中使用多個 jQuery 事件,例如單擊、雙擊、滑鼠進入、滑鼠離開、懸停等。
示例
可以嘗試執行以下程式碼,瞭解如何使用 jQuery 多個事件處理同一個功能
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").on({
mouseenter: function(){
$(this).css("background-color", "gray");
},
mouseleave: function(){
$(this).css("background-color", "red");
},
dblclick: function(){
$(this).css("background-color", "yellow");
}
});
});
</script>
</head>
<body>
<p>Click, double click and move the mouse pointer.</p>
</body>
</html>
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP