jQuery得到.text()但不是span中的文本 [英] jQuery get .text() but not the text in span

查看:62
本文介绍了jQuery得到.text()但不是span中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是我的jQuery部分,为我的页面制作菜单。

Hi guys 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');

        });
    });
}

我想要的是获取项目。 menuTitle < a 中,但在< span> 之前

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

目前我这样做:

$('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;
});;

还有更好的方法吗?

推荐答案

是的,您只能选择元素的文本内容,如下所示:

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);

这篇关于jQuery得到.text()但不是span中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆