最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery get .text() but not the text in span - Stack Overflow

programmeradmin10浏览0评论

This is my jQuery part that makes my menu for my pages.

function fetchmenus() {
    $.getJSON('sys/classes/fetch.php?proccess=1', function(status) {
        // get line status
        $.each(status, function(i, item) {
            if (item.id == "1") {
                active = "class='active'";
                lastpageid = item.href;
            }
            else {
                active = "class='nonactive'";
            }
            $('<li id="' + item.href + '" ' + active + '><a href="#' + item.href + '">' + item.menuTitle + '<span class="menuspan" id="' + item.href + '"></span></a></li>').appendTo('#menuarea ul#mainmenu');

        });
    });
}

What I want to do is get the item.menuTitle in the <a but before the <span>.

Currently I do it on this way:

$('ul#mainmenu li').live('click', function(event) {
    //alert(this.id);
    $("li#" + lastpageid).removeClass();
    fetchpage(this.id);

    $("#largemenutop").html($(this).text());

    $("li#" + this.id).addClass("active");
    lastpageid = this.id;
});;

Is there a better way to do this?

This is my jQuery part that makes my menu for my pages.

function fetchmenus() {
    $.getJSON('sys/classes/fetch.php?proccess=1', function(status) {
        // get line status
        $.each(status, function(i, item) {
            if (item.id == "1") {
                active = "class='active'";
                lastpageid = item.href;
            }
            else {
                active = "class='nonactive'";
            }
            $('<li id="' + item.href + '" ' + active + '><a href="#' + item.href + '">' + item.menuTitle + '<span class="menuspan" id="' + item.href + '"></span></a></li>').appendTo('#menuarea ul#mainmenu');

        });
    });
}

What I want to do is get the item.menuTitle in the <a but before the <span>.

Currently I do it on this way:

$('ul#mainmenu li').live('click', function(event) {
    //alert(this.id);
    $("li#" + lastpageid).removeClass();
    fetchpage(this.id);

    $("#largemenutop").html($(this).text());

    $("li#" + this.id).addClass("active");
    lastpageid = this.id;
});;

Is there a better way to do this?

Share Improve this question edited Jan 1, 2021 at 12:17 peterh 1 asked May 6, 2011 at 14:43 RussellHarrowerRussellHarrower 6,82025 gold badges112 silver badges224 bronze badges 3
  • its not clear to me what you are asking. are you looking for a better way to build up the html for the menu? Or to access the title of the anchor once the menu is built already? – Jeff Commented May 6, 2011 at 15:02
  • here is a sample <li id="messages"><a href="#messages">Messages<span class="menuspan" id="bmessages">4</span></a></li> What is happening it when I click that link it shows the following Messages4 when I want it to show Messages – RussellHarrower Commented May 6, 2011 at 15:34
  • Possible duplicate of Using .text() to retrieve only text not nested in child tags – Stephan Weinhold Commented Dec 26, 2016 at 9:19
Add a ment  | 

2 Answers 2

Reset to default 23

Nice solution Herman, though it can be reduced to something like this:

JS

$('li a').contents().filter(function() {
    return this.nodeType == 3;
}).text();

HTML

<li><a href="#">Apple<span>hi</span> Juice</a></li>

Will return Apple Juice

Fiddle: http://jsfiddle/49sHa/1/

Yes, you can select only the text contents of the element, like this:

 var text = '';
 $('a').contents().each(function(){
    if(this.nodeType === 3){
     text += this.wholeText;
    }
 });
 $("#largemenutop").html(text);
发布评论

评论列表(0)

  1. 暂无评论