简体   繁体   English

使用Javascript DOM添加事件侦听器

[英]Adding event listeners with Javascript DOM

I'm having trouble adding eventListener to through javascript. 我在通过javascript添加eventListener时遇到问题。 Ok, I have a function that creates 4 anchor elements. 好的,我有一个创建4个锚元素的函数。 I want to add an event to them onmouseover that calls a function to change their backkground color. 我想在他们的onmouseover上添加一个事件,调用一个函数来改变他们的backkground颜色。 Here's the code (look at the next to last line of createAnchor() to find the relevant code line. 这是代码(查看createAnchor()的最后一行的下一行以查找相关的代码行。

function createAanchor(index) { 
            var a = document.createElement("a");
            var text = getText(index);
            var a = document.createElement("a");
            var t = document.createTextNode(text);
            a.href = getHref(index);
            a.appendChild(t);
            a.style.textAlign = "center";
            a.style.fontSize = "1.2em";
            a.style.color = "white";
            a.style.fontFamily = "arial";
            a.style.fontWeight = "bold";
            a.style.textDecoration = "none";
            a.style.lineHeight = "238px";
            a.style.width = "238px";
            a.style.margin = "5px";
            a.style.background = eightColors(index);
            a.style.position = "absolute";
            a.addEventListener("onmouseover", changeColor());
            return a;
    }

    function changeColor() {
        alert("EVENT WORKING");
    }

Ok here's the problem. 好的,这是问题所在。 When the function gets to a.addEventListener("onmouseover", changeColor()); 当函数到达a.addEventListener("onmouseover", changeColor()); the function changeColors() executes, but it does not execute later on onmouseover Why is this? function changeColors()执行,但它稍后不会在onmouseover上执行为什么会这样?

  1. There is no such event onmouseover , the event is called mouseover . onmouseover上没有这样的事件,该事件被称为mouseover
  2. You have to pass a function reference to addEventlistener . 您必须将函数引用传递给addEventlistener () calls the function, as you already noticed, so... don't call it. ()调用函数,正如您已经注意到的那样,所以...不要调用它。

This is how it should be: 这应该是这样的:

a.addEventListener("mouseover", changeColor);

I recommend to read the excellent articles about event handling on quirksmode.org. 我建议在quirksmode.org上阅读有关事件处理的优秀文章

It's because you wrote changeColors() instead of just changeColors . 这是因为你编写了changeColors()而不仅仅是changeColors The () tell JavaScript to call the function. ()告诉JavaScript调用该函数。

In other words, changeColors by itself is a reference to the function, while changeColors() refers to the function and then calls it . 换句话说, changeColors本身是对函数的引用,而changeColors()引用该函数然后调用它 The result of the function call (the return value from the function) is what's ultimately passed to addEventListener() . 函数调用的结果(函数的返回值)是最终传递给addEventListener()

Ok I think we need to understand when to use the prefix "on" with the event type. 好的我认为我们需要了解何时使用事件类型的前缀“on”。 In IE 8 or less then IE8 we use attachEvent and detachEvent which are equivalent to addEventListener and removeEventListener. 在IE 8或更低版本的IE8中,我们使用attachEvent和detachEvent,它们等同于addEventListener和removeEventListener。 There are some differences which are not required for this question. 这个问题不需要一些差异。

While using attachEvent the event type is prefixed with "on" but in addEventListener no prefix is used. 使用attachEvent时,事件类型的前缀为“on”,但在addEventListener中没有使用前缀。

hence, 因此,

    elem.attachEvent("onclick",listener); // <= IE8

    elem.addEventListener("click",listener,[,useCapture]); // other browsers

I've needed to do something like that too. 我也需要这样做。 I have an infoWindow in my map and I need to handle a click event on paragraph in that infoWindow. 我在地图中有一个infoWindow,我需要处理该infoWindow中段落的click事件。 So I did it like this: 所以我这样做了:

google.maps.event.addListener(infoWindow, 'domready', function() 
     {
        paragraph = document.getElementById("idOfThatParagraph");
        paragraph.addEventListener("click", function()
        {
          //actions that I need to do
        });
    });

It works for me. 这个对我有用。 So I hope it will help someone :) 所以我希望它会帮助别人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM