2012-05-04 137 views
0

我試圖將節點的html保存到localstorage。這是我想要做的。首先查找「強壯」的節點,如果這個節點的html是「AM」,那麼我會從span類節點獲得「time」類的時間。它似乎工作,但不是我想要的方式。相反,它會從第一個跨度獲取時間字符串,而不是在「AM」的html「strong」節點之上的跨度。我希望這是明確的,如果不是我會盡我所能解釋。找到特定節點後查找節點

HTML

<div> 
<span class="time">Today 07:11 PM</span> 
<strong>PM</strong> 
</div> 

<div> 
<span class="time">Today 07:21 PM</span> 
<strong>AM</strong> 
</div> 

代碼

checkTime = setInterval(function(){ 
    // get html of the 1st node 
    var firstNode = $("strong").html(); 

    // if 1st node html is "AM" save time to local storage 
    if (firstNode == "AM") { 
     var userClock = $("span.time").html(); 
     localStorage.setItem('clock', userClock); 
    } 
}, 1000); 

回答

0
checkTime = setInterval(function(){ 
    // get html of the 1st node 
    var firstNode = $('strong:contains("AM")'); 

    // if 1st node html is "AM" save time to local storage 
     var userClock = firstNode.prev('span.time').html(); 
     localStorage.setItem('clock', userClock); 
}, 1000); 

Live Proof

+0

謝謝你的偉大工程 –

+0

@ Mr.1.0歡迎 – thecodeparadox