如何基于clicked元素使用jquery.on(' click')获取父选择器 [英] How to get parent selector with jquery.on('click'), base on the clicked element
本文介绍了如何基于clicked元素使用jquery.on(' click')获取父选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jQuery.on()
,并且具有以下代码:
I'm using jQuery.on()
, and have the following code:
$('.parent').on('click', '.link',function (event) {
console.log($(this)); // Logs '.link'
});
如何访问触发点击的".parent"元素?
How can i access to the '.parent' element in which the click was triggered?
$(this)
返回'.link'元素
$(this)
returns the '.link' element
推荐答案
您可以使用最近的():
You can use closest():
$(this).closest('.parent');
或使用:$(event.delegateTarget)
,它将考虑多个嵌套的.parent
元素.
Or use: $(event.delegateTarget)
which will take into account multiple nested .parent
elements.
如果您的元素是锚标记,则必须防止默认行为.
If your element is an anchor tag, you have surely to prevent default behaviour.
这篇关于如何基于clicked元素使用jquery.on(' click')获取父选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文