Home  >  Article  >  Web Front-end  >  Detailed explanation of Javascript event-driven programming_javascript skills

Detailed explanation of Javascript event-driven programming_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:22:171613browse

1. Basic Overview
JS uses an event-driven mechanism to respond to user operations. That is to say, when a user operates an html element, a time will be generated, which will drive certain functions to process.
PS: This method is very similar to the event listening mechanism in Java GUI. Both need to register the listener and then handle the listener, but the implementation method is different.

2. Event-driven principle

  • Event source: Where the event is generated (html element)
  • Events: Click/mouse operation/keyboard operation, etc.
  • Event object: When an event occurs, an event object may be generated. The time object will encapsulate the time information and pass it to the event handler
  • Event handler: Code that responds to user events

Case:

<html> 
  <head> 
    <script type="text/javascript"> 
      function test1(e){ 
        window.alert("x=" + e.clientX + " y=" + e.clientY); 
      } 
      function test2(e){ 
        window.alert("x=" + e.clientX + " y=" + e.clientY); 
      } 
      function test3(e){ 
        window.alert(new Date().toLocaleString()); 
      } 
      function test4(e){ 
        if(e.value == "red"){ 
          div1.style.backgroundColor = "red"; 
        } else if (e.value == "black"){ 
          div1.style.backgroundColor = "black"; 
        } 
      } 
    </script> 
  </head> 
  <body> 
    <input type="button" onclick="test1(event)" value="button1"> 
    <input type="button" onmouseover="test2(event)" value="button2"> 
    <input type="button" onclick="test3(event)" value="button3"> 
    <div id="div1" style="width: 400px; height: 300px; background-color: red"></div> 
    <input type="button" onclick="test4(this)" value="red"> 
    <input type="button" onclick="test4(this)" value="black"> 
  </body> 
</html> 
  • JS event classification
  • Mouse events
  • click dblclick mousedown mouseout mouseover mouseup mousemove etc.
  • Keyboard events
  • keydown keypress keyup etc.
  • HTML events
  • window’s onload unload error abort text box’s select change, etc.
  • Other events
  • Events generated during the operation of some special objects on the page

Case 1: Monitor mouse click events and be able to display the location x, y of the mouse click

<html> 
   <head> 
   <script> 
   function test1(e){ 
     window.alert("x="+e.clientX+"y="+e.clientY); 
   } 
   </script> 
   </head> 
   <body onmousedown="test1(event)"> 
   </body> 
</html> 

After clicking the browser, the coordinates will be displayed (some browsers may not be valid)

Case 2: Click the button and the picture turns red and black
Method: JS access internal css

//js如何访问css属性,来改变外观 
<html> 
  <head> 
  <script> 
    function test3(e){ 
      var pic=document.getElementById("pic"); 
      if(e.value=="红色"){ 
        pic.style.backgroundColor="red"; 
      } 
      else if(e.value=="黑色"){ 
        pic.style.backgroundColor="black"; 
      } 
   } 
  </script> 
  </head> 
  <body > 
    <div id="pic" style="border:1;background-color:red;width:300px;height:300px"></div> 
    <input type="button" onclick="test3(this)" value="红色"> 
    <input type="button" onclick="test3(this)" value="黑色"> 
  </body> 
</html> 

Method: JS access external css (this method may not be applicable to all browsers)

event2.css
.style { 
  border:1; 
  background-color:red; 
  width:300px; 
  height:300px; 
} 
event2.html
<html> 
  <head> 
  <script> 
    function test3(e){ 
      //取连接的第一个css文件的内容用0 
      var ocssRules=document.styleSheets[0].rules; 
      //从ocssRules取出你希望的样式 
      var style=ocssRules[0];//这里面的0表示event2.css文件中第一个规则 
      if(e.value=="黑色"){ 
        style.style.backgroundColor="black"; 
       } 
       else if(e.value=="红色"){ 
         style.style.backgroundColor="red"; 
       } 
    } 
  </script> 
  </head> 
  <body> 
    <div class="style"></div> 
    <input type="button" onclick="test3(this)" value="红色"> 
    <input type="button" onclick="test3(this)" value="黑色"> 
   </body> 
</html> 

Case 3: What is the core of the current browser? (Distinguish ie6/7/8/ Firefox, etc.)

<script language="javascript"> 
   if(window.XMLHttpRequest) 
   { //Mozilla, Safari, IE7,IE8  
      if(!window.ActiveXObject) 
    {  // Mozilla, Safari,  
      alert('Mozilla, Safari');  
    } 
     else 
     {  
      alert('IE7 .8');  
    }  
   } 
   else  
   {  
     alert('IE6');  
   }  
</script> 

Case 4: An event can be monitored by multiple functions

<html> 
  <head> 
  function test(e){ 
    window.alert("fss"); 
  } 
  function test1(e){ 
    window.alert("sfdsdf"); 
  } 
  </script> 
  </head> 
  <body> 
    <div class="style"></div> 
    <input type="button" onclick="test(this),test1(this)" value="红色"> 
  </body> 
</html> 

Case 5: Prevent users from copying web content by clicking the right mouse button menu and selecting web content

<html> 
  <script type="text/javascript"> 
    function test(){ 
      //window.alert("没有菜单"); 
      return false; 
    } 
    function test2(){ 
      //window.alert("全选不行"); 
      return false; 
    }   
  </script>  
  </head>  
  <!--body元素响应oncontextmenu,onselectstart事件 --> 
  <body oncontextmenu="return test()" onselectstart="return test2()">   
    内容 
  </body> 
</html> 

The next article will share with you a simple and comprehensive case: Simple Calculator, I hope you don’t miss it.

There is much more to Javascript event-driven programming than this. I hope this article will be helpful for everyone to learn JavaScript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn