2014-09-29 39 views
0

我的代碼一直運行良好,直到遇到問題。我的XML數據可能並不總是按照火車票號碼的順序排列,所以我必須根據下次出發時間附加火車票號碼。我不知道如何組成我的'if'語句字符串來不破壞代碼。如果使用XML語句

這是我到目前爲止有:

$(data).find('service time:eq(0)').each(function() { 
      var nextTrain = $(this).find('name').text(); 

      if ($(this).find('name:contains(2)').each(function() { 
       $('#slot01 #rt01a').append($('<h1 class="bullet"><span class="tl123"><span class="bgw">-</span>2</span></h1>')); 
      })); 

      if ($(this).find('name:contains(3)').each(function() { 
       $('#slot01 #rt01a').append($('<h1 class="bullet"><span class="tl123"><span class="bgw">-</span>3</span></h1>')); 
      })); 

      if ($(this).find('name:contains(4)').each(function() { 
       $('#slot01 #rt01a').append($('<h1 class="bullet"><span class="tl456"><span class="bgw">-</span>4</span></h1>')); 
      })); 

      if ($(this).find('name:contains(5)').each(function() { 
       $('#slot01 #rt01a').append($('<h1 class="bullet"><span class="tl456"><span class="bgw">-</span>5</span></h1>')); 
      })); 
     }); 

我從來都沒有很好的與「如果」的語句,所以我敢肯定,這可怕的編寫。我的最終目標是採取下一組火車,並找出列車x是否在x = 2,3,4,5附加適當的文本。

回答

0

我的措辭是錯誤的。我的xml文件的搜索沒有按照正確的順序,我使用.each代替.length

  $(data).find('REAL train:eq(0)').each(function() { 
      var firstTrainN = $(this).find('name').text() 

      if ($(this).find('name:contains(2)').length) { 
       $('#slot01 #rt01a .bullet').append($('<span class="tl123"><span class="bgw">-</span>2</span>')); 
      }; 

      if ($(this).find('name:contains(3)').length) { 
       $('#slot01 #rt01a .bullet').append($('<span class="tl123"><span class="bgw">-</span>3</span>')); 
      }; 

      if ($(this).find('name:contains(4)').length) { 
       $('#slot01 #rt01a .bullet').append($('<span class="tl456"><span class="bgw">-</span>4</span>')); 
      }; 

      if ($(this).find('name:contains(5)').length) { 
       $('#slot01 #rt01a .bullet').append($('<span class="tl456"><span class="bgw">-</span>5</span>')); 
      }; 
     });