'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - How can I check on which element the event was triggered? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I check on which element the event was triggered? - Stack Overflow

programmeradmin0浏览0评论
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;
    .......
}
发布评论

评论列表(0)

  1. 暂无评论