Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM addEventListener() method

HTML DOM addEventListener() method

Document Object Reference Document Object

Example

Add a click event to the document. When the user clicks anywhere in the document, on the element with id="demo",

output "Hello World":

document

.

addEventListener

(

"

click

"

,

function

(

)

{

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

"

Hello World

"

;

}

)

;

Try it »


Definition and usage

The document.addEventListener() method is used to add an event handler to the document.

Tip: You can use the document.removeEventListener() The method is used to remove the event handler added by the addEventListener() method.

**Tip:** Use * element.*addEventListener() The method adds an event handler to a specified element.


Browser support

The numbers in the table indicate the first browser version that supports this method.

Method
addEventListener() 1.0 9.0 1.0 1.0 7.0

Note: Internet Explorer 8 and earlier versions of IE do not support the addEventListener() method, nor do Opera 7.0 and earlier versions of Opera. However, for these browser versions you can use the attachEvent() method to add an event handler (see "More Examples" for cross-browser compatibility issues).


Syntax

document.addEventListener(

event

,

function

,

useCapture

)

Parameter value

Parameters Description
event Required. A string describing the event name.
 **注意:** 不要使用 "on" 前缀。例如,使用 "click" 来取代 "onclick"。       

   

 ** Tip: ** For all HTML DOM events, you can view our complete [HTML DOM Event Object Reference Manual](/jsref/dom-obj-event.html). |

| function | Required. The function to be executed after the event is triggered.

 当事件触发时,事件对象会作为第一个参数传入函数。 事件对象的类型取决于特定的事件。例如, "click" 事件属于 MouseEvent(鼠标事件) 对象。 |

| useCapture | Optional. A Boolean value specifying whether the event is executed during the capturing or bubbling phase.

 可能值:     
  • true - The event handler is executed during the capturing phase
  • false - Default. The event handler is executed during the bubbling phase
  • |

    Technical Details

    DOM version: DOM Level 2 Events
    Return Value: No return value
    Modification record: In Firefox 6 and Opera 11.60 the useCapture parameter is optional. (It has always been optional in Chrome, IE, and Safari).

    More examples

    Example

    You can reference an external function by its function name:

    document.addEventListener("click", myFunction);

    function myFunction() {

        document.getElementById("demo").innerHTML = "Hello World";

    }

    Try it »

    Example

    You can add many events to the document, and the added events will not overwrite existing events.

    This example demonstrates how to add two click events to the document:

    document.addEventListener("click", myFunction);

    document.addEventListener("click", someOtherFunction);

    Try it »

    Example

    You can add different types of events to the document.

    This example demonstrates how to add multiple events to the document:

    document.addEventListener("mouseover", myFunction);

    document.addEventListener("click", someOtherFunction);

    document.addEventListener("mouseout", someOtherFunction);

    Try it »

    Example

    When passing parameter values, use an "anonymous function" to call a function with parameters:

    document.addEventListener("click", function() {

        myFunction(p1, p2);

    });

    Try it »

    Example

    Modify the background of the element:

    document.addEventListener("click", function(){

        document.body.style.backgroundColor = "red";

    });

    Try it »

    Example

    Use the removeEventListener() method to remove the event handler added via the addEventListener() method:

    // Add an event handler to the document

    document.addEventListener("mousemove", myFunction);

    // Remove the event handler of the document

    document.removeEventListener("mousemove", myFunction);

    Try it »

    Example

    If the browser does not support the addEventListener() method, you can use the attachEvent() method instead.

    The following example demonstrates a cross-browser solution:

    if (document.addEventListener) {                // All major browsers, except IE 8 and earlier IE versions

        document.addEventListener("click", myFunction);

    } else if (document.attachEvent) {             // IE 8 and earlier IE versions

        document.attachEvent("onclick", myFunction);

    }

    Try it »


    Related pages

    JavaScript Tutorial:HTML DOM EventListener

    JavaScript Reference Manual:element.addEventListener()

    Document Object Reference Document Object