gpt4 book ai didi

javascript - 简单的 dom 类选择器

转载 作者:行者123 更新时间:2023-11-28 16:33:34 25 4
gpt4 key购买 nike

<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com