简体   繁体   中英

HTML Anchor Tag With Italic Tag

I have some html.

<a href="#">
   <i class="some-bg" />
   Some Text
</a>

And some Javascript.

$("a").bind("touchstart", function (e) {
   e.preventDefault();

   console.log("Tag: " + e.target);
   console.log("Tag Name: " + e.target.tagName);
});

The response is.

Tag: [object HTMLElement]
Tag Name: I

Why? Shouldn't it be anchor?

UPDATED

$("a, a *").bind(function() {
    e.stopPropagation();

    // other stuff
});

Will this do the trick?

Why?

Because you touched the <i> (and then the event bubbled up to the <a> ).

Shouldn't it be anchor?

No. Use currentTarget if you want the element to which the event is bound rather then the one which actually triggered the event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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