← Back to jQuery Course | Chapter 3: Events | Lesson 2 of 11

Mouse Events

click event तब चलता है जब user किसी element को click करता है।
Syntax
javascript
$(selector).click(function () {
  // code
});
$(selector).dblclick(handler);
$(selector).mouseenter(handler);
$(selector).mouseleave(handler);

Click

click event में एक handler bind करना user के उस element पर mouse button दबाने और छोड़ने के instant आपका function चला देता है।

उदाहरण: Click

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click</button>
    <script>
      $("#btn").on("click", function() {
      console.log("Clicked");
      });
    </script>
  </body>
</html>

Double Click

dblclick event सिर्फ तभी fire होता है जब दो clicks जल्दी succession में register हों -- अकेला single click इसे trigger नहीं करेगा, जो इसे उन actions के लिए उपयोगी बनाता है जिन्हें आप accidental single clicks से guard करना चाहते हैं।

उदाहरण: Double Click

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Double-click</button>
    <script>
      $("#btn").on("dblclick", function() {
      console.log("Double clicked");
      });
    </script>
  </body>
</html>

Mouse Down और Up

mousedown उस moment fire होता है जब button दबाया जाता है, और mouseup तब fire होता है जब इसे release किया जाता है -- click को दो अलग events में split करना आपको custom drag या press-and-hold interactions बनाने देता है।

उदाहरण: Mouse Down and Up

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Press and hold</button>
    <script>
      $("#btn").on("mousedown", function() {
      console.log("Button pressed");
      });
      $("#btn").on("mouseup", function() {
      console.log("Button released");
      });
    </script>
  </body>
</html>

Mouse Enter और Leave

mouseenter एक बार fire होता है जब pointer किसी element की boundary में crosses करता है, और mouseleave एक बार fire होता है जब यह बाहर जाता है -- mouseover/mouseout के उलट, वे अंदर के child elements के लिए दोबारा fire नहीं होते।

उदाहरण: Mouse Enter and Leave

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Hover me</div>
    <script>
      $("#box").on("mouseenter", function() {
      console.log("Entered");
      });
      $("#box").on("mouseleave", function() {
      console.log("Left");
      });
    </script>
  </body>
</html>

Mouse Move

mousemove बार-बार fire होता है, प्रति second कई बार, जब तक pointer element के ऊपर रहता है -- cursor position track करने के लिए उपयोगी लेकिन अगर handler भारी काम करता है तो costly।

उदाहरण: Mouse Move

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Move mouse here</div>
    <script>
      $("#box").on("mousemove", function(event) {
      console.log(event.pageX, event.pageY);
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि user के एक बार click करने पर dblclick चलेगा; यह सिर्फ दो जल्दी clicks के बाद fire होता है, और उसी element पर एक single-click handler भी दोनों clicks के लिए fire होता है।
  2. किसी parent पर mouseover और mouseout इस्तेमाल करना और pointer के child elements में crossing करने पर repeated firing पाना, जहाँ mouseenter और mouseleave एक बार fire होते हैं।
  3. यह भूल जाना कि mousedown mouseup और click से पहले fire होता है, इसलिए वहाँ भारी logic चलाना user के click पूरा करने से पहले चल सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.