问题描述
好的,所以我想做的是获取具有类名'no-js'的元素并将其替换为'js',所以我不知道该怎么做,我在google上尝试了一下,但是什么都找不到那么现在有没有人怎么做呢?
ok so what I'm trying to do is to get the element's with the class name 'no-js' and replace it with 'js', so I have NO idea how to do this, I tried google around But couldn't find anything,so does anyone now how to do this?
我要制作的是这个菜单,当您单击加号按钮时,它具有此下拉菜单,但是如果禁用了javascript,我希望它在CSS悬停时显示(我已经做到了)
what I'm trying to make Is this menu that when you click on a Plus button it has this drop down menu, but if javascript is disabled I want it to show on hover with css ( i've already done that )
我已将我的代码放在JsFiddle上,这里是链接: http://jsfiddle.net/MrPumpkin22/v9dcC/10/
i've put My code on JsFiddle, heres the link:http://jsfiddle.net/MrPumpkin22/v9dcC/10/
谢谢.
推荐答案
您需要迭代返回的元素,并在每个元素上替换该类名称的该部分:
You need to iterate the returned elements and replace that portion of the class name on each one:
var els = [].slice.apply(document.getElementsByClassName("no-js"));
for (var i = 0; i < els.length; i++) {
els[i].className = els[i].className.replace(/ *\bno-js\b/g, "js");
}
请注意, getElementsByClassName
返回一个活动列表",这就是为什么必须首先制作返回值的副本(使用 [].slice
)并进行迭代的原因该列表).
Note that getElementsByClassName
returns a "live list", which is why it's necessary to first make a copy of the return value (using [].slice
) and iterate that list instead).
这篇关于如何用javascript替换类HTML名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!