Jquery对象的迭代

时间:2016-07-05 10:29:50

标签: jquery

我有以下对象:

<li class="departure-details">
 <ul>
   <li class="departure"> 
     <span class="departure-date">Wed 07/13/16</span>
     <span class="departure-time">11:12</span>
  </li>
  <li class="arrival">
    <span class="arrival-date">Wed 07/13/16 </span>
    <span class="arrival-time">11:12</span>
  </li>
</ul>
<ul>
   <li class="departure"> 
     <span class="departure-date">Wed 07/13/16</span>
     <span class="departure-time">11:12</span>
  </li>
  <li class="arrival">
    <span class="arrival-date">Wed 07/13/16 </span>
    <span class="arrival-time">11:12</span>
  </li>
</ul>
</li>

我想使用jQuery .each函数迭代它。请帮忙。提前感谢:)

2 个答案:

答案 0 :(得分:0)

尝试:

&#13;
&#13;
$(function(){
    $("ul li").find("span").each(function() {
       console.log($(this).text()); // loop through all li and print text of span
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<li class="departure-details">
 <ul>
   <li class="departure"> 
     <span class="departure-date">Wed 07/13/16</span>
     <span class="departure-time">11:12</span>
  </li>
  <li class="arrival">
    <span class="arrival-date">Wed 07/13/16 </span>
    <span class="arrival-time">11:12</span>
  </li>
</ul>
<ul>
   <li class="departure"> 
     <span class="departure-date">Wed 07/13/16</span>
     <span class="departure-time">11:12</span>
  </li>
  <li class="arrival">
    <span class="arrival-date">Wed 07/13/16 </span>
    <span class="arrival-time">11:12</span>
  </li>
</ul>
</li>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这应该这样做:

$(".departure-details ul").each(function() {// code here});