Common Javascript monitor events

Common Javascript monitor events

Event listener mechanism

  • Concept: Some components is performed certain operations, trigger the execution of some code.
    • Event: certain operations. Such as: click, double click, press the keyboard, the mouse moved
    • Event Source: components. Such as: the button text input box ...
    • Listener: Code.
    • Sign Monitor: The combination of events, event source, the listener. When an event occurs on the event source, then trigger the execution of a listener code.
  • Common events:
    1. Click event:

      1. onclick: Click Event
      2. ondblclick: Double-click event
    2. Focus Events

      1. onblur: lose focus
      2. onfocus: element receives the focus.
    3. Load event:

      1. onload: a page or an image has finished loading.
    4. Mouse events:

      1. onmousedown mouse button is pressed.
      2. onmouseup mouse button is released.
      3. onmousemove mouse is moved.
      4. onmouseover mouse over an element.
      5. onmouseout mouse is moved off an element.
  1. Keyboard events:
    1. onkeydown a keyboard key is pressed.
    2. onkeyup a keyboard key is released.
    3. onkeypress a keyboard key is pressed and released.

    1. Select and change

      1. Onchange field content is changed.
      2. onselect text is selected.
    2. Form events:

      1. onsubmit confirmation button is clicked.
      2. onreset reset button is clicked.
  • How to bind event

    1. Directly on the html tag, attribute specifies the event (operation), is the attribute value js code

      1. Event: onclick- click event
    2. Element object acquired by js, specify the event properties, set a function

    • Code:
<body>
		<img id="light" src="img/off.gif"  onclick="fun();">
		<img id="light2" src="img/off.gif">
		
		<script>
		    function fun(){
		        alert('我被点了');
		        alert('我又被点了');
		    }
		
		    function fun2(){
		        alert('咋老点我?');
		    }
		
		    //1.获取light2对象
		    var light2 = document.getElementById("light2");
		    //2.绑定事件
	    light2.onclick = fun2;
		</script>
	</body>
Published 31 original articles · won praise 0 · Views 237

Guess you like

Origin blog.csdn.net/fyyxc/article/details/104301371