<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]
哪个不存在
最佳答案
让我猜猜。您想要的是:
var elems = document.getElementsByClassName("time");
for(var timeIndex = 0; timeIndex < elems.length; ++timeIndex)
{
elems[timeIndex].innerHTML = "Reformatted time";
}
(如果不是您想要的,请澄清问题)
关于javascript - 简单的dom类选择器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4911136/