听起来很简单,呵呵.我发现很多答案,但都使用jQuery或ProtoType.我想要简单的JavaScript.它不应该那么难,但JavaScript不是我的事; 没有中央文件意味着寻找年龄而不是找到我想要的东西.
请考虑以下HTML代码段:
<div class="central_0"> .. </div>
<div class="central_1"> .. </div>
<div class="central_2"> .. </div>
Run Code Online (Sandbox Code Playgroud)
现在我想使用JavaScript来处理那些DIV.
function processDivElements()
{
// search for relevant DIV classes
var divArray = document.getElementsByClass.regex('/^central_.*$/');
// do stuff with the DIV elements found
foreach (divArray as divElement)
{
divElement.style.background = '#f00';
};
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我翻译成适当的纯 JavaScript吗?我使用的是类,而不是ID.我更喜欢使用正则表达式.