admin管理员组

文章数量:1334826

addLBEvent : function()
    {
        var amount = this.imageList.length;
        for(var i = 0;i < amount;i++)
        {
            if(this.imageList[i].addEventListener)
            {
                this.imageList[i].addEventListener("click",this.startLB,false);
            }
            /* 
                IE<9-part
            */
        }
    },

startLB : function(src)
    {


    }

I'd like to know which element triggered the event. If I'd do this in the HTML-Code I'd write something like onlick="startLB(this.src)" for example. How can I do such a thing with addEventListener? I've already tried `addEventListener("click","myobjectname.startLB(this.src)" but it didn't work.

And sorry for my bad English

addLBEvent : function()
    {
        var amount = this.imageList.length;
        for(var i = 0;i < amount;i++)
        {
            if(this.imageList[i].addEventListener)
            {
                this.imageList[i].addEventListener("click",this.startLB,false);
            }
            /* 
                IE<9-part
            */
        }
    },

startLB : function(src)
    {


    }

I'd like to know which element triggered the event. If I'd do this in the HTML-Code I'd write something like onlick="startLB(this.src)" for example. How can I do such a thing with addEventListener? I've already tried `addEventListener("click","myobjectname.startLB(this.src)" but it didn't work.

And sorry for my bad English

Share Improve this question asked May 18, 2012 at 7:50 SindharaSindhara 1,48314 silver badges20 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 5

An event object is passed in as the first argument to any event handler.

The event object as a target property identifying the element to which the event applies.

addLBEvent : function(event) {
    console.log(event.target);
}

https://developer.mozilla/en/DOM/event.target

You can access a reference to the element that triggered the event...

var elementThatTriggeredEvent = e.target || e.srcElement;

...assuming that e is the reference to the event.

If you want to know which element was clicked, use event.target.

If you want to know which element you had the handler on, use event.currentTarget or, in most cases, this. addEventListener will call your handler with this set to the element on which you called addEventListener.

Note the distinction. For instance, if you had this markup:

<div id="foo"><span id="bar">Hi there</span></div>

...and this code:

document.getElementById("foo").addEventListener('click', function(event) {
    alert(this.id);
    alert(event.target.id)
}, false);

...then if the user clicks the text "Hi there", this will be the div but event.target will be the span.

Live example | source

See how this is the element you hooked the event on, and event.target is the element on which it fired (and then it bubbled up to the div).


Note that addEventListener isn't available on older versions of IE; you have to use attachEvent instead. attachEvent doesn't ensure that this is set to the element on which you hooked it, so beware that difference between APIs. To smooth things like that out, you might look to any decent library, like jQuery, YUI, Closure, or any of several others.

Use this:

startLB : function(src)
    {
       var element = src.target.tagName;
       alert("element >> "+element);
    }   

I think it's best for you to bind it like this:

var that = this;
this.imageList[i].addEventListener("click",function() {
    that.startLB(this.src);
},false);

this will bee the event target, so we have to access the object somehow, I named that that

try this...

addLBEvent : function(e)
{
    if (!e) e = event;
    e = e.srcElement || e.target;
    .......
}

本文标签: javascriptHow can I check on which element the event was triggeredStack Overflow