我正在尝试将节点的html保存到localstorage。这是我想要做的。首先查找“强”节点,如果此节点的html为“AM”,那么我将从具有“time”类的span节点获取时间。它似乎工作,但不是我想要的方式。相反,它从第一个跨度获取时间字符串,而不是“h”为“AM”的“强”节点上方的跨度。我希望这很清楚,如果不是我会尽力解释。
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 :(得分: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 强>