开发者

simple dom class selector

<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++;
}

How can I detect the last element of a class, and then make 51 a variable?

//
//*****************************EDITS HERE**************************************
//
//Example:
  var LAST_ELEMENT_OF_CLASS = someMagicCode("time");

and then

while (timeIndex < LAST_ELEMENT_OF_CLASS +1){...}

Note it currently doesn't work because it tries to evaluate

document.getElementsByClassName("time")[5]

Which doesn't exist


Let me guess. What you want is this:

var elems = document.getElementsByClassName("time");
for(var timeIndex = 0; timeIndex < elems.length; ++timeIndex)
{
  elems[timeIndex].innerHTML = "Reformatted time";
}

(if that's not what you want please clarify question)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜