从具有特定类的HTML a-Tag获取文本

时间:2014-09-02 14:39:14

标签: jquery html

我试图获取所选文字。在我的案例;只是'香蕉'! 我不想得到它的yummie'!

我的 HTML代码:

 <ul>
    <li class="static"><a tabindex="0" class=
        "static menu-item ms-core-listMenu-item ms-displayInline ms-navedit-linkNode" href=
        "/de/SitePages/info.aspx"><span class=
        "additional-background ms-navedit-flyoutArrow"><span class=
    "menu-item-text">Orange</span></span></a></li>

    <li class="static selected"><a tabindex="0" class=
        "static selected menu-item ms-core-listMenu-item ms-displayInline ms-core-listMenu-selected ms-navedit-linkNode"
        href="/de/SitePages/wt.aspx"><span class=
        "additional-background ms-navedit-flyoutArrow"><span class=
        "menu-item-text">Banana</span><span class="ms-hidden">Its
    yummie</span></span></a></li>

    <li class="static"><a tabindex="0" class=
        "static menu-item ms-core-listMenu-item ms-displayInline ms-navedit-linkNode" href=
        "/de/SitePages/pw.aspx"><span class=
        "additional-background ms-navedit-flyoutArrow"><span class=
    "menu-item-text">Apple</span></span></a></li>

    <li class="static"><a tabindex="0" class=
        "static menu-item ms-core-listMenu-item ms-displayInline ms-navedit-linkNode" href=
        "/de/SitePages/bt.aspx"><span class=
        "additional-background ms-navedit-flyoutArrow"><span class=
    "menu-item-text">Grapes</span></span></a></li>
</ul>

我的 jQuery代码

var v = $('a', 'li.selected').first().text();

以下代码返回:

  

BananaIt&#s; yummie

我只想获得&#39; 香蕉&#39;

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您应该尝试以下方法:

var v = $('.menu-item-text', 'li.selected').first().text();

答案 1 :(得分:0)

只需将类&#34; .menu-tem-text&#34; 添加到您的jQuery代码中:

$(a li.selected .menu-item-text).first.text();