js 怎么通过class改变样式

 我来答
码路行者
2019-03-06 · 码路行者,以码为生。专注前端WEB。
码路行者
采纳数:74 获赞数:7541

向TA提问 私信TA
展开全部

js通过class改变样式,可以使用Dom的className属性设置元素的样式。完整示例代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>测试页面</title>

<style type="text/css">

.themeCls {

color: #000;

background-color: #f60;

line-height: 25px;

}

</style>

</head>

<body style="background-color:#ccc;">

<span id="theme">这是一段测试文本<br />用来测试js通过class改变样式</span>

<script type="text/javascript">

var domTheme = document.getElementById("theme");

theme.className = "themeCls";

</script>

</body>

</html>

具体操作步骤如下:

1、新建一个html文件,命名为t.html。

2、打开t.html。

3、在t.html中写入html结构代码,其中设置需要添加class类的元素的ID为“theme”。代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>测试页面</title>

</head>

<body style="background-color:#ccc;">

<span id="theme">这是一段测试文本<br />用来测试js通过class改变样式</span>

</body>

</html>

4、设置一个css类,命名为”themeCls”,用于在javascript操作时给元素添加clsss。”themeCls”类为了方便观察效果,设置css规则为字体颜色为黑色#000,背景为橙色#f60,行高为25像素。代码如下:

<style type="text/css">

.themeCls {

color: #000;

background-color: #f60;

line-height: 25px;

}

</style>

5、编写javascript代码,获取ID为“theme”的元素并设置元素的class类为“themeCls”,代码如下:

<script type="text/javascript">

var domTheme = document.getElementById("theme");

domTheme .className = "themeCls";

</script>

6、打开浏览器,浏览t.html页面,发现页面中”这是一段测试文本用来测试js通过class改变样式”这一段文本字体颜色呈现黑色,背景呈现橙色,说明我们为元素添加class类“themeCls”成功了。

幻翼高达Zero
2019-06-21 · TA获得超过1.7万个赞
知道答主
回答量:499
采纳率:0%
帮助的人:7.1万
展开全部

需要准备的材料分别是:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html的<script>标签中,输入js代码:$('body').css('background-color', 'blue');。

3、浏览器运行index.html页面,此时发现body的背景颜色class被调整为了蓝色。

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
阿铁SuperIron
2017-05-31 · TA获得超过243个赞
知道小有建树答主
回答量:184
采纳率:100%
帮助的人:120万
展开全部
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.class{
color: #000;
}
</style>
</head>
<body>

<p class="test">原来是黑色的, 通过js变蓝</p>

<script>
window.onload = function(){

var e = document.getElementsByClassName("test");
e[0].style.color = "blue";

}
</script>
</body>
</html>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2017-05-31
展开全部
<table>
<tbody>
<tr>
<td>js实现class的样式的修改、添加、删除</td>
<td>
<a e_value="g_sn" ename="商品编码" class="goods_sale_property" href="javascript:void(0);">商品编码</a>
<a e_value="pdt_sn" ename="商品货号" class="goods_sale_property" href="javascript:void(0);">商品货号</a>
<a e_value="pdt_name" ename="规格名称" class="goods_sale_property" href="javascript:void(0);">规格名称</a>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td><a onclick="selectAll()" style="color:#F00">全选</a> </td>
<td><a onclick="selectNotAll()" style="color:#F00">全不选</a></td>
</tr>
</tbody>
</table>

<script>
$('.goods_sale_property').click(function(){//单独a标签点击添加class
if($(this).hasClass('goods_sale_property_checked')){
$(this).removeClass('goods_sale_property_checked');
}else{
$(this).addClass('goods_sale_property_checked');
}
});
function selectAll(){//全选添class
$('.goods_sale_property').each(function(i){
$(this).addClass('goods_sale_property_checked');
});
}
function selectNotAll(){//全选删除class
$('.goods_sale_property').each(function(i){
$(this).removeClass('goods_sale_property_checked');
});
}
</script>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2017-05-31
展开全部
.a{width:100px;height:100px;background-color:red;}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式