jQuery 最接近返回未定义

发布于 2024-11-29 04:09:05 字数 3256 浏览 0 评论 0原文

我有一个生成的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

粉红×色少女 2024-12-06 04:09:05

首先,“查看个人资料”链接不在 h3 内,而是在 h3 的同级中。尝试这个

当您单击查看个人资料链接时尝试这个

accindex = $(this).closest("div").prev("h3").find("a").attr("accindex");

注意:在上面的代码中 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

accindex = $(this).closest("div").prev("h3").find("a").attr("accindex");

Note: In the above code this represents the anchor which is clicked.

呆橘 2024-12-06 04:09:05

这是一个工作小提琴: http://jsfiddle.net/mrchief/M6PcW/

$('a.link').click(function() {
    alert($(this).closest('div').prev('h3').children('a').attr('accindex'));
    return false;
});

我将您的标记修改为将类添加到所需的 href(以方便选择)。
例如 更新个人资料

Here's a working fiddle: http://jsfiddle.net/mrchief/M6PcW/

$('a.link').click(function() {
    alert($(this).closest('div').prev('h3').children('a').attr('accindex'));
    return false;
});

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>

赠我空喜 2024-12-06 04:09:05

看起来您需要使用“查找”或“子项”。

因此,要获取锚标记中的属性,您需要执行以下操作(当使用 H3 的上下文时):

$("h3 a").attr("accindex");

或者

$("h3").find("a").attr("accindex");

或者

$("h3").children("a").attr("accindex");

我猜测您的点击事件使用锚标记作为其选择,因此您可能只需要执行以下操作:

$(this).attr("accindex");

单击“基本”这里。
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):

$("h3 a").attr("accindex");

or

$("h3").find("a").attr("accindex");

or

$("h3").children("a").attr("accindex");

I am guessing your click event uses the anchor tag as its selection so you may just need to do:

$(this).attr("accindex");

Click on 'Basic' here.
http://jsfiddle.net/hns2H/1/

末が日狂欢 2024-12-06 04:09:05

“查看个人资料”链接不是

的子链接,这就是为什么 parents()closest() 没有工作。需要从链接向上遍历DOM到

    ,再到

    ,然后就可以得到

    了。

假设 this 是链接:

$(this).closest('ul.navigation-treeview').closest('div').prev('h3');

应该得到您想要的

从那里获取随附的 及其 accindex。

var acc = $(this).closest('ul.navigation-treeview').closest('div').prev('h3').find('a');
acc.attr('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 why parents() and closest() 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:

$(this).closest('ul.navigation-treeview').closest('div').prev('h3');

should get the <h3> you want.

From there, get the enclosed <a> and its accindex.

var acc = $(this).closest('ul.navigation-treeview').closest('div').prev('h3').find('a');
acc.attr('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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文