本文介绍: pink;</</</</

元素样式设置六种方法

1、对象.style
2、对象.className
3、对象.setAttribute(“style”)
4、对象.setAttribute(“class”)
5、对象.style.setProperty(“CSS属性”, “CSS属性值”)
6、对象.style.cssText

<style&gt;
      .pink {
        background-color: pink;
      }
    </style&gt;
<body&gt;
    <div id="box"&gt;盒子</div&gt;
    <button id="change"&gt;变色</button&gt;
  </body&gt;

js文件获取元素

点击按钮时给div盒子添加背景颜色

var box = document.getElementById("box");
document.getElementById('change').addEventListener('click',function(){
}
设置的六种方法操作

事件处理程序中(函数内)书写

//1、对象.style
     box.style.backgroundColor = 'pink'
// 2、对象.className
     box.className = 'pink'
// 3、对象.setAttribute("style")
box.setAttribute('style','background-color:pink')
// 4、对象.setAttribute("class")
    box.setAttribute('class','pink')
 //5、对象.style.setProperty("CSS属性", "CSS属性值")
 box.style.setProperty('background-color','pink')
// 6、对象.style.cssText
  box.style.cssText = 'background-color:pink'

原文地址:https://blog.csdn.net/weixin_52452081/article/details/129734211

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_45086.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注