我正在尝试将节点的 html 保存到本地存储。这是我想要做的。首先查找“强”节点,如果该节点的 html 仅是“AM”,那么我会从具有“时间”类的 span 节点获取时间。它似乎有效,但不是我想要的方式。相反,它从第一个跨度而不是“强”节点上方的跨度获取时间字符串,html 为“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);