<ul>
<li> <div class="time"> at 2011-02-05 17:44:28</div></li>
<li> <div class="time"> at 2011-02-05 17:40:20</div></li>
<li> <div class="time"> at 2011-02-05 17:15:11</div></li>
<li> <div class="time"> at 2011-02-05 17:04:14</div></li>
<li> <div class="time"> at 2011-02-05 15:54:56</div></li>
</ul>
//Reformat timeStamps
var timeIndex = 0;
while (timeIndex < 51)
{
var timeIndexHTML = document.getElementsByClassName("time")[timeIndex].innerHTML;
timeIndexHTML = "Reformatted time";
document.getElementsByClassName("time")[timeIndex].innerHTML = timeIndexHTML;
timeIndex++;
}
如何检测类的最后一个元素,然后将51设为变量?
//
//*****************************EDITS HERE**************************************
//
//Example:
var LAST_ELEMENT_OF_CLASS = someMagicCode("time");
然后
while (timeIndex < LAST_ELEMENT_OF_CLASS +1){...}
注意它目前无效,因为它试图评估
document.getElementsByClassName("time")[5]
哪个不存在
答案 0 :(得分:4)
让我猜一下。你想要的是这个:
var elems = document.getElementsByClassName("time");
for(var timeIndex = 0; timeIndex < elems.length; ++timeIndex)
{
elems[timeIndex].innerHTML = "Reformatted time";
}
(如果那不是你想要的,请澄清问题)