简体   繁体   中英

How to get event target's parent with jQuery?

I use jQuery's event object to get the target's parent. The code is:

event.target.parentElement

In Chrome it works, but in Firefox, it doesn't. I use alert() to print the output.

event.target is an URL in both Chrome and Firefox; but event.target.parentElement in Chrome is [Element object], and in Firefox it is null .

What should I do to solve this problem? I still wonder if IE will have this problem?

Thanks.

要访问父级,您可以执行以下操作:


var par = $(event.target).parent();

To retrieve an element from an inline link's target, it may be better to use the hash attribute of target, thusly:

(function($) {
    parent = $(event.target.hash).parent();
})(jQuery);

And btw, instead of alert, you can get much more information using console.log with either firefox/firebug or chrome.

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