Home  >  Article  >  Web Front-end  >  Execute script when mouse button is pressed on HTML element?

Execute script when mouse button is pressed on HTML element?

王林
王林forward
2023-08-29 20:53:141316browse

Execute script when mouse button is pressed on HTML element?

Use the onmousedown attribute in HTML to execute a script when the mouse button is pressed on an element.

Example

You can try running the following code to implement the onmousedown attribute -

<!DOCTYPE html>
<html>
   <body>
      <h3 id = "myid" onmousedown = "mouseDown()" onmouseup = "mouseUp()">
         This is demo heading.
      </h3>
      <p>Click above and then release.</p>
      <script>
         function mouseDown() {
            document.getElementById("myid").style.color = "yellow";
         }
         function mouseUp() {
            document.getElementById("myid").style.color = "blue";
         }
      </script>
   </body>
</html>

The above is the detailed content of Execute script when mouse button is pressed on HTML element?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete