2013-11-15 20 views
0

我的HTML:Jquery仅输出选定的html,但不输出它的子项?

<ul id="seasonsDropDown"> 
    <li>FALL 2013 HAUTE COUTURE 
    <ul class="designers"> 
     <li>Alexander Wang</li> 
     <li>BCBG MAX Azria</li> 
     <li>Diane con Fursternberg</li> 
     <li>Diane Von De</li> 
    </ul> 
    </li> 
    <li>SPRING 2013 HAUTE COUTURE</li> 
    <li>SPRING 2033 HAUTE COUTURE</li> 
    <li>SPRING 2093 COUTURE</li> 

什么jQuery的方法,我应该使用,如果我只是想选择/输出字:

"FALL 2013 HAUTE COUTURE" 

不是UL名单?

现在如果我使用:

console.log($("#seasonsDropDown").html()); 

它会给我一切,包括HTML。

+2

它看起来像这个问题是一个重复:http://stackoverflow.com/questions/3442394/jquery-使用文本检索只有文本没有嵌套在子标签 –

+2

请参阅$('#seasonsDropDown li:first')。contents()。filter(function(){ return this.nodeType == 3 })。text()'like http://jsfiddle.net/arunpjohny/y4D25/1/ –

回答

1

试试这个我没有检查它,但..

$(function() { 
    $('#seasonsDropDown').each(function(i, items_list){ 
     var myText = ""; 

     $(items_list).find('li').each(function(j, li){ 
      alert(li.text()); 
     }) 

     alert(myText); 

    }); 
}; 
0

jQuery将为了知道要选择哪一个项目需要一个选择。最好的办法是添加这样的选择:

$("#seasonsDropDown li").click(function(){ 
    $(this).addClass("selected"); 
}); 

之后,你就可以选择它作为如下:

console.log($("#seasonsDropDown .active").html()); 

如果你只是需要一个onClick事件,你可以做如下:

$("#seasonsDropDown li").click(function(){ 
    console.log($(this).html()); 
}); 
1
$("#seasonsDropDown li").html().substr(0,$("#seasonsDropDown li").html().indexOf('<')); 
1
console.log(
    $('#seasonsDropDown li:first').contents()[0] 
); 

你可能需要修剪返回的字符串。

1

你的问题与此类似question

jQuery的方法如下

$("li:eq(0)").clone().children().remove().end().text()