addClass到getElementsByClassName数组 [英] addClass to getElementsByClassName array

查看:318
本文介绍了addClass到getElementsByClassName数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于对.getElementsByClassName感到沮丧,我需要一些帮助. 我有一个带有类路径的svg映射.现在,我需要列出具有特定类的所有内容,并添加另一个类.现在我有

I need a little help as i am getting frustrated with .getElementsByClassName. I have an svg map that has paths with classes. I now need to list all with a certain class and add another class. Right now i have

var testarray = (document).getElementsByClassName("currentclass");
for(var i = 0; i < testarray.length; i++)
{
   testarray.item(i).className("classtobeadded");
}

这将向我返回未定义不是函数"错误.我试过$(document),(document),(jQuery),我试过$(.currentclass").addClass(),我试过很多组合,但都没有成功.你们能告诉我我在做什么错吗?

This returns me a "undefined is not a function" error. I've tried $(document), (document), (jQuery), i've tried $(".currentclass").addClass(), i've tried lots of combinations without success. Can you guys tell what i am doing wrong?

谢谢!

推荐答案

您有一些语法错误,应该可以解决:

You have a couple syntax errors, this should get it done:

var testarray = document.getElementsByClassName("currentclass");
for(var i = 0; i < testarray.length; i++)
{
    testarray[i].className += "classtobeadded";
}

或者由于您正在使用jQuery,您可以执行以下操作:

Or since you're using jQuery you can do:

$(".currentclass").each(function() {
    $(this).addClass("classtobeadded");
});

这篇关于addClass到getElementsByClassName数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆