您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

为指定元素增加样式的js代码_javascript技巧

2026/2/3 6:52:10发布11次查看
这个函数主要是为指定的元素添加样式。相当于jquery中的addclass(class)--为每个匹配的元素添加指定的类名。
在jquery中要求 class (string) : 一个或多个要添加到元素中的css类名,请用空格分开。而这个函数也是有这样的要求。
复制代码 代码如下:
div{ border:1px solid #ccc; width:200px; height:200px;}
.a{ background:#900; }
.b{ font-size:30px; font-weight:bold;}
复制代码 代码如下:
function addclass(elements, value)
{
if (!elements.classname) {
elements.classname = value;
}
else
{
newclass = elements.classname;
newclass += ;
newclass += value;
elements.classname = newclass;
}
}
window.onload = function ()
{
var test = document.getelementbyid('test');
alert(test.classname);
addclass(test, 'a b');
//addclass(test, 'b');
}
这里是测试层
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product