最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Should I be using "this" or "event.target" in javascript? - Stack Overflow

programmeradmin2浏览0评论

I have created a simple div demonstration below, that will display none once click.

<div id="three" onclick="toggle2(event);return false;" style="background:#303; color:#FFF;">

function toggle2(e) {

    var textx = (e.target) ? e.target : e.srcElement;
    textx.style.display = "none";

    console.log(e.target);
} 

my question is what is the difference if I replace

<div id="three" onclick="toggle2(event);return false;" style="background:#303; color:#FFF;">

with

<div id="three" onclick="toggle2(this);return false;" style="background:#303; color:#FFF;">

both of them work fine for my example abovee.....

I have created a simple div demonstration below, that will display none once click.

<div id="three" onclick="toggle2(event);return false;" style="background:#303; color:#FFF;">

function toggle2(e) {

    var textx = (e.target) ? e.target : e.srcElement;
    textx.style.display = "none";

    console.log(e.target);
} 

my question is what is the difference if I replace

<div id="three" onclick="toggle2(event);return false;" style="background:#303; color:#FFF;">

with

<div id="three" onclick="toggle2(this);return false;" style="background:#303; color:#FFF;">

both of them work fine for my example abovee.....

Share Improve this question edited Jun 12, 2013 at 8:31 Vincent Chua asked Jun 12, 2013 at 8:03 Vincent ChuaVincent Chua 1,0096 gold badges21 silver badges41 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

It may well be that they are exactly the same. This depends on the HTML. In this case, this will always be the div element. this refers to the element where the event is captured. event.target, however, points to the element where the event originated.

If the element has no children, they will always be the same. If, however, you had something like this:

<div id="three" onclick="toggle2(event);return false;" style="background:#303; color:#FFF;">
    <span>Line 1</span>
    Line 2
</div>

then they may be different. A click on Line 1 will cause event.target to be the span element, so only that element will be hidden.

Unless you specifically want to point to the element where the event originated, it's more intuitive to use this.

You usually use e.target when "e" is the event like a click passed in parameter.

When you pass this as a parameter, this is the reference to the DOM node in which there is the javascript method. So here, "this" references the div.

And as you have a click event on your div, when you click on it, it is considered as an event, that's why this and e.target both work.

Moreover, "this" will always refer to you div, whereas "e.target" will reference an element you clicked in the div.

I think isn't necessary to pass this as argument on onclick event, you can use this direct to the function.

The event refers to the event currently being fired. Now in a browser the events are bubbled up from the element on which the event is fired to its parent until it reaches the document root. More at: What is event bubbling and capturing?

In your example, the event points to the click event and the event.target is the div and this refers to the div itself. If you add a child element to the div and click on the element then the event.target will point to the child element and this will still refer to the div.

发布评论

评论列表(0)

  1. 暂无评论