jQuery get .text() but not the text in span

后端 未结 2 799
刺人心
刺人心 2020-12-31 11:41

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)          


        
相关标签:
2条回答
  • 2020-12-31 12:35

    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 讨论(0)
  • 2020-12-31 12:37

    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.net/49sHa/1/

    0 讨论(0)
提交回复
热议问题