jQuery 最接近返回未定义
我有一个生成的 jqueryui 手风琴代码,如下所示:
<div id="treeview-accordion">
<h3><a href="#" accindex="0">Basic</a></h3>
<div>
<ul class="navigation-treeview treeview-sanjo" id="yw0">
<li><span style="font-weight:bold"><a href="/sanjo/site/index"> Home</a></span>
<ul>
<li><span> Profile</span>
<ul>
<li><span><a href="/sanjo/user/profile"> View Profile</a></span></li>
<li><span><a href="/sanjo/user/profile/edit"> Update Profile</a></span></li>
<li><span><a href="/sanjo/user/profile/changepassword"> Change Password</a></span></li>
</ul>
</li>
</ul>
</li>
<li><span> Personnel Management</span>
<ul>
<li><span><a href="/sanjo/user/admin"> Manage Personnel</a></span>
<ul>
<li><span><a href="/sanjo/user"> List Personnel</a></span></li>
<li><span> Add Personnel</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<h3><a href="#" accindex="0">Basic2</a></h3>
<div>
<ul class="navigation-treeview treeview-sanjo" id="yw01">
<li><span style="font-weight:bold"><a href="/sanjo/site/index2"> Home2</a></span>
<ul>
<li><span> Profile</span>
<ul>
<li><span><a href="/sanjo/user/profile2"> View Profile2</a></span></li>
<li><span><a href="/sanjo/user/profile/edit2"> Update Profile2</a></span></li>
<li><span><a href="/sanjo/user/profile/changepassword2"> Change Password2</a></span></li>
</ul>
</li>
</ul>
</li>
<li><span> Personnel Management2</span>
<ul>
<li><span><a href="/sanjo/user/admin2"> Manage Personnel2</a></span>
<ul>
<li><span><a href="/sanjo/user2"> List Personnel2</a></span></li>
<li><span> Add Personnel2</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
我想要实现的是根据单击的链接获取适当的“h3”标签的属性“accindex”。
例如,我点击“查看个人资料”,由于它位于“h3”(基本)下,所以我想获取其accindex属性的值,即“0”。
我尝试使用closest(),但它返回“未定义”。我也不能使用parents()。
我怎样才能完成这样的任务呢?请帮助我。
谢谢。
I have a generated jqueryui accordion code like this:
<div id="treeview-accordion">
<h3><a href="#" accindex="0">Basic</a></h3>
<div>
<ul class="navigation-treeview treeview-sanjo" id="yw0">
<li><span style="font-weight:bold"><a href="/sanjo/site/index"> Home</a></span>
<ul>
<li><span> Profile</span>
<ul>
<li><span><a href="/sanjo/user/profile"> View Profile</a></span></li>
<li><span><a href="/sanjo/user/profile/edit"> Update Profile</a></span></li>
<li><span><a href="/sanjo/user/profile/changepassword"> Change Password</a></span></li>
</ul>
</li>
</ul>
</li>
<li><span> Personnel Management</span>
<ul>
<li><span><a href="/sanjo/user/admin"> Manage Personnel</a></span>
<ul>
<li><span><a href="/sanjo/user"> List Personnel</a></span></li>
<li><span> Add Personnel</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<h3><a href="#" accindex="0">Basic2</a></h3>
<div>
<ul class="navigation-treeview treeview-sanjo" id="yw01">
<li><span style="font-weight:bold"><a href="/sanjo/site/index2"> Home2</a></span>
<ul>
<li><span> Profile</span>
<ul>
<li><span><a href="/sanjo/user/profile2"> View Profile2</a></span></li>
<li><span><a href="/sanjo/user/profile/edit2"> Update Profile2</a></span></li>
<li><span><a href="/sanjo/user/profile/changepassword2"> Change Password2</a></span></li>
</ul>
</li>
</ul>
</li>
<li><span> Personnel Management2</span>
<ul>
<li><span><a href="/sanjo/user/admin2"> Manage Personnel2</a></span>
<ul>
<li><span><a href="/sanjo/user2"> List Personnel2</a></span></li>
<li><span> Add Personnel2</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
and what I want to achieve is to get the attribute "accindex" of the appropriate 'h3' tag depending on the link clicked.
For example,I clicked on "View Profile", and since it is located under the "h3" (Basic), I want to get the value of its accindex attribute, which is "0".
I tried to use closest(), but it is returning "undefined". I can't use parents() either.
How could I accomplish such task? Kindly help me out.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,“查看个人资料”链接不在 h3 内,而是在 h3 的同级中。尝试这个
当您单击查看个人资料链接时尝试这个
注意:在上面的代码中
this
代表被单击的锚点。First of all "View Profile" link is not inside h3, its in the sibling of h3. Try this
When you click on view profile link try this
Note: In the above code
this
represents the anchor which is clicked.这是一个工作小提琴: http://jsfiddle.net/mrchief/M6PcW/
我将您的标记修改为将类添加到所需的 href(以方便选择)。
例如
更新个人资料
Here's a working fiddle: http://jsfiddle.net/mrchief/M6PcW/
I modified your markup to add a class to desired href (to make the selection easy).
E.g.
<a class="link" href="/sanjo/user/profile/edit"> Update Profile</a>
看起来您需要使用“查找”或“子项”。
因此,要获取锚标记中的属性,您需要执行以下操作(当使用 H3 的上下文时):
或者
或者
我猜测您的点击事件使用锚标记作为其选择,因此您可能只需要执行以下操作:
单击“基本”这里。
http://jsfiddle.net/hns2H/1/
It looks like you need to use 'find' or 'children'.
So to get the attribute in the anchor tag you would do (when using the context of the H3):
or
or
I am guessing your click event uses the anchor tag as its selection so you may just need to do:
Click on 'Basic' here.
http://jsfiddle.net/hns2H/1/
“查看个人资料”链接不是
的子链接,这就是为什么
parents()
和closest()
没有工作。需要从链接向上遍历DOM到,再到
,然后就可以得到
了。
。假设
this
是链接:应该得到您想要的
。
从那里获取随附的
及其 accindex。
演示: http://jsfiddle.net/yWsDx/
编辑:
$(this).closest( 'div').prev('h3').find('a')
应该没问题,.closest('ul.navigation-treeview')
不是必需的。The 'View profile' link is not a child of the
<h3>
, this is whyparents()
andclosest()
did not work. You need to traverse up the DOM from the link to the<ul>
, then the<div>
, then you can get the<h3>
.Assuming
this
is the link:should get the
<h3>
you want.From there, get the enclosed
<a>
and its accindex.DEMO: http://jsfiddle.net/yWsDx/
EDIT:
$(this).closest('div').prev('h3').find('a')
should be fine, the.closest('ul.navigation-treeview')
isn't necessary.