js中设置样式的几种方式(setAttribute,className)

23 篇文章 2 订阅

以设置背景色为例子


可以用单一的backgroundColor
可以用className
可以用setAttributesetAttribute removeAttribute)为一组

实例代码(实现效果点击一个按钮,就把整个背景色替换,点击其他的就换其他的)

 

<html>

<head>

<title></title>

<style type="text/css">
    .bg{
        background-color : "blue" ;
    }    
</style>

<script language="javascript" type="text/javascript">
    window.onload = function(){
    
        $("redC").onclick = function(){
            document.body.removeAttribute("className","bg");
            document.body.style.backgroundColor = "red";
        }
         $("blueC").onclick = function(){
            document.body.style.backgroundColor = "";
           
            document.body.setAttribute("className","bg");
//            document.body.className = "bg";
        }
        
        $("pinkC").onclick = function(){
          document.body.removeAttribute("className","bg");
            document.body.style.backgroundColor = "pink";
        }
        
        $("blackC").onclick = function(){
          document.body.removeAttribute("className","bg");
            document.body.style.backgroundColor = "black";
        }
    }
    
    function $(uid){
        return document.getElementById(uid);
    }
</script>

</head>

<body>
<input type="button" value="red" id="redC"/>
<input type="button" value="blue" id="blueC"/>
<input type="button" value="pink" id="pinkC"/>
<input type="button" value="black" id="blackC"/>
</body>

</html>


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
package com.example.supplier.service; import com.example.supplier.entity.User; import java.util.List; public interface UserService { //查询全部 public List<User> selectAll(); } package com.example.supplier.dao; import com.example.supplier.entity.SysLogInfo; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; import java.util.Map; @Repository @Mapper public interface SysLogInfoDao { // 分页查询数据 List<SysLogInfo> listSysLogInfo(Map map); // 查询总数量 Integer getLogInfoCount(Map map); } <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <title>创建组合</title> [removed][removed] [removed][removed] </head> <body <div class="chose2" ms-controller="fenye" id="fenye"> <ul class="neirong" > <li ms-repeat="datalist"><p><span>{{el.id}}</span><span >{{el.name}}</span></p></li> </ul> <!--//数据--> <div id="yema" </div> </div> [removed] var yema=document.getElementById("yema"); //<![CDATA[ function fun(){ var top=[[${top}]]; console.log(top); for(var i=1;i<3;i++){ var a=document.createElement("a"); a.setAttribute("id",i); a.innerText=i; a.href="#"; a. a. fun(){ change(this.id); tian(this.id); }; yema.appendChild(a); }//生成前两个页码,这两个页码是始终都存在的 var span1=document.createElement("span"); span1.innerText="..."; span1.setAttribute("id","span1"); yema.appendChild(span1);//生成省略号,根据情况进行判断显示与否 for (var i = 3; i <top-1; i++) { var p = document.createElement("a"); p.setAttribute("id", i); p.innerText = i; p.href="#"; p. p. fun(){ change(this.id); tian(this.id); }; yema.appendChild(p); } var span2=document.createElement("span"); span2.innerText="..."; span2.setAttribute("id","span2"); yema.appendChild(span2);//生成后一个省略号 for(var i=top-1;i<=top;i++){ var p=document.createElement("a"); p.setAttribute("id",i); p.innerText=i; p.href="#"; p. p. fun(){ change(this.id); tian(this.id); }; yema.appendChild(p); }//生成后两个页码,这两个页码也始终都存在 tian(1);//调用函数,进行初始化页面处理 viewmodel.request(); } function tian(flag){ var top=[[${top}]]; console.log(top); //使用js隐藏属性 var span1=document.getElementById("span1"); var span2=document.getElementById("span2"); var value = flag; if(value>=5){ span1.style.display=""; }//如果是第5个页码以上,省略号将显示 else span1.style.display="none"; for(var i=3;i<top-1;i++){ var p=document.getElementById(i); if(i>value-2&&i<parseInt(value)+2){ p.style.display="";//显示 }//显示当前页码以及当前页码后两个页码 else p.style.display="none";//隐藏 } if(value<=top-3) span2.style.display=""; else span2.style.display="none"; } //]]> function change(flag){ viewmodel.number=flag; viewmodel.request(); } var viewmodel = avalon.define({ //id必须和页面上定义的ms-controller名字相同,否则无法控制页面 $id: "fenye", number:1, datalist: {}, request: function () { $.ajax({ type: "POST", url:"/data", data:{"number":viewmodel.number}, success: function (data) { viewmodel.datalist = data; } }); } }); avalon.scan(); [removed] </body> </html> <!--分页显示数据-->

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小傅哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值