简体   繁体   English

$(e.target)是 而不是包含 <div> … </div>

[英]$(e.target) is <i></i> and not the containing <div>…</div>

I have been working on a large script which checks the data-action and now I've implemented a hover to show some icons; 我一直在研究检查数据操作的大型脚本,现在我实现了悬停以显示一些图标。 like so: 像这样:

<div class="Flag" data-action="mail-flag"><i class="fa fa-flag" aria-hidden="true"></i></div>

However with this not having plain text within and instead an icon, if ($(e.target).attr('data-action')) { does not work as $(e.target) is the <i></i> . 但是,由于其中没有纯文本,而是一个图标, if ($(e.target).attr('data-action')) {不能作为$(e.target)作为<i></i>

How can I get if ($(e.target).attr('data-action')) { to work on the <div>...</div> without adding the data-action="mail-flag" to the <i></i> ? 如何获得if ($(e.target).attr('data-action')) {<div>...</div>而不将data-action="mail-flag"<i></i>吗?

You can check the element, then its parent. 您可以检查元素,然后检查其父元素。 First, check if 首先,检查是否

$(e.target).attr('data-action')

exists. 存在。 If it does not, use 如果没有,请使用

$(e.target).parents(".Flag").attr('data-action')

You can use event delegation to access the attribute value. 您可以使用事件委托来访问属性值。

$('div[data-action]').on('mouseenter', 'i.fa', function(e){
    console.log($(e.delegateTarget).data('action'))
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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