0

在我的 html 页面上,我有多个菜单。单击订单项时,我想从选择来源的菜单中获取父锚标记​​的 id 属性。我的 html 和 jquery 代码如下。

//HTML

<ul id="menuA">
    <li>
        <a href="#" id="ageGroup" class="age">Age Group</a>
            <ul>
                <li name="ageGroup"><a href="#">18-21</a></li>
                <li name="ageGroup"><a href="#">21-30</a></li>
                <li name="ageGroup"><a href="#">30-40</a></li>
                <li name="ageGroup"><a href="#">40-50</a></li>
                <li name="ageGroup"><a href="#">50-60</a></li>
                <li name="ageGroup"><a href="#">60-70</a></li>
                <li name="ageGroup"><a href="#">70-80</a></li>
                <li name="ageGroup"><a href="#">80-90</a></li>
                <li name="ageGroup"><a href="#">90-100</a></li>
            </ul>

    </li>
    </ul>

//JQUERY

$(function(){

    $('#menuA').menu({ 

select: function(event, ui){

      var choice = ui.item.text(); //get text of menu selection

    //here is where I want to get the id attribute of the anchor tag. What I have isn't working. id is coming up undefined.

    var id = ui.item.parents('a').attr('id'); 


     }
});
4

1 回答 1

1

您将使用以下命令找到相应的标签 ID:

var id = ui.item.parents('li').find('a').attr('id');

您之前的尝试不起作用,因为父选择器正在返回您的列表项 () 而不是在主 li 中搜索标签。

于 2013-06-15T22:27:02.843 回答