0

我的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。

4

5 回答 5

1

试试这个我还没有检查过..

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

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

        alert(myText);

    });
};
于 2013-11-15T08:29:24.550 回答
1
$("#seasonsDropDown li").html().substr(0,$("#seasonsDropDown li").html().indexOf('<') );
于 2013-11-15T08:31:29.227 回答
1

你的问题类似于这个问题

JQuery方法如下

$("li:eq(0)").clone().children().remove().end().text()
于 2013-11-15T08:52:11.843 回答
1
console.log(
  $('#seasonsDropDown li:first').contents()[0]
);

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

于 2013-11-15T08:39:06.907 回答
0

jQuery 需要一个选择器才能知道您要选择哪个项目。最好的办法是添加这样的选择器:

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

之后,您将能够选择它,如下所示:

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

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

$("#seasonsDropDown li").click(function(){
   console.log($(this).html());
});
于 2013-11-15T08:30:20.103 回答