jQuery:获取父代,父代ID? [英] jQuery: get parent, parent id?

查看:92
本文介绍了jQuery:获取父代,父代ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<ul id ="myList">
<li><a href="www.example.com">link</a></li>
</ul>

如何使用jQuery获取ul(myList)的ID?单击链接时触发我的j脚本事件.我已经尝试过:

How can I get the id of the ul (myList) using jQuery? My j-script event is triggered when the a link is clicked. I have tried:

$(this).parent().attr('id');

但是它得到了li的ID,我需要再高一点,我也无法将点击附加到li上.

But it gets the id of the li, I need to go one higher, and I cant attach the click to the li either.

推荐答案

$(this).parent().parent().attr('id');

您将如何获取父母的父母的ID.

Is how you would get the id of the parent's parent.

$(this).closest('ul').attr('id');

这是针对您案件的更简单的解决方案.

Is a more foolproof solution for your case.

这篇关于jQuery:获取父代,父代ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆