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

如何使用jQuery获取ul(myList)的id?单击a链接时会触发My j-script事件.我试过:

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

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');

是你获取父母身份的方式.

编辑:

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

Is a more foolproof solution for your case.

Jquery相关问答推荐

如何用each替换元素的attr href

通过 Ajax POST 将按钮值从 HTML 发送到 Flask 时出现空数据

从文本区域获取值

在正则表达式字符括号中包含连字符?

Jquery如何通过数组中的属性查找对象

jQuery ajax 调用默认超时值

输入类型=文件的jQuery更改方法

在 contenteditable div 中的插入符号处插入 html

如何禁用在div内单击

jQuery计算所有文本字段中的值的总和

将参数附加到 URL 而不刷新

jquery 从特定表单获取所有输入

从父 node 中删除所有子 node ?

获取跨域 iframe 的 DOM 内容

如何使 jQuery Contains 不区分大小写,包括 jQuery 1.8+?

如何使锚链接不可点击或禁用?

如何复制 pinterest.com 的绝对 div 堆叠布局

将十六进制转换为 RGBA

更改 url 而不使用 javascript 重定向

如何在 DOM 中移动 iFrame 而不会丢失其状态?