如何使用 jQuery 多個事件觸發相同函式?
若要使用多個事件觸發同一函式,可將 jQuery on() 方法與多個事件結合使用,例如點選、雙擊、滑鼠進入、滑鼠離開、懸停等。
例項
你可嘗試執行以下程式碼,瞭解如何使用 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>
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP