4.9 am例题

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>
        <style type="text/css">
            *{
                margin:0px auto;}
            #kuang{
                width:100px;
                height:50px;}
            .list{
                width:100px;
                height:50px;
                background-color:#03C;
                text-align:center;
                vertical-align:middle;
                line-height:50px;
                color:#fff;
                border:1px solid #CCC}    
            
        </style>
    </head>
    
    <body>
        <div id="kuang">
            <div class="list" οnclick="Xuan(this)" οnmοuseοver="Bian(this)" οnmοuseοut="Hui()">深圳</div>
            <div class="list" οnclick="Xuan(this)" οnmοuseοver="Bian(this)" οnmοuseοut="Hui()">北京</div>
            <div class="list" οnclick="Xuan(this)" οnmοuseοver="Bian(this)" οnmοuseοut="Hui()">上海</div>
        </div>
        
       <!--    <div id="l" style="width:100%; height:200px; background-color:red;"></div>
        <div id="caidan" style="width:100%; height:100px; background-color:blue;"></div>
        <div style="width:100%; height:1000px; background-color:#000;"></div>
        <input type="button" value="an" οnclick="Hui()" />-->
        
    </body>
    <script type="text/javascript">
        function Xuan(a)
        {
            var list = document.getElementsByClassName("list");
            //清一下(回复原样式)
            for(var i=0;i<list.length;i++)
            {
                list[i].style.backgroundColor = "#03C";    
                list[i].removeAttribute("as");
            }
            //选择变色
            a.style.backgroundColor = "red";
            a.setAttribute("as",1);    
        }
        function Bian(a)
        {
            var list = document.getElementsByClassName("list");
            //清一下(回复原样式)
            for(var i=0;i<list.length;i++)
            {
                if(list[i].getAttribute("as")!=1)
                {
                    list[i].style.backgroundColor = "#03C";        
                }
                
            }
            //选择变色
            a.style.backgroundColor = "red";        
        }
        function Hui()
        {
            var list = document.getElementsByClassName("list");
            //清一下(回复原样式)
            for(var i=0;i<list.length;i++)
            {
                if(list[i].getAttribute("as")!=1)
                {
                    list[i].style.backgroundColor = "#03C";        
                }
                
            }    
        }
        
        /*function Hui()
        {
            window.scrollTo(0,200);    
        }*/
        
        /*window.onscroll = function()
        {
            var c = document.getElementById("caidan");    
            //var ch = c.style.height;
            var l = document.getElementById("l");    
            var lh = l.style.height;
            //parseInt(ch);
            if(window.scrollY >= parseInt(lh))
            {
                //超出的范围操作    
                c.style.position = "fixed";
                c.style.top = "0px";
            }
            else
            {
                //没有超出的范围操作
                c.style.position = "relative";
            }*/
        }
    </script>
</html>

这是这个例题的最终效果,鼠标移上可以变色,移开在变回来,点击可以选中而且变色。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>无标题文档</title>
 <style type="text/css">
 *{
 margin:0px auto;}
 #kuang{
 width:100px;
 height:50px;}
 .list{
 width:100px;
 height:50px;
 background-color:#03C;
 text-align:center;
 vertical-align:middle;
 line-height:50px;
 color:#fff;
 border:1px solid #CCC}
  
 </style>
 </head>
  
 <body>
 <div id="kuang">
 <div class="list" onclick="Xuan(this)" onmouseover="Bian(this)" onmouseout="Hui()">深圳</div>
 <div class="list" onclick="Xuan(this)" onmouseover="Bian(this)" onmouseout="Hui()">北京</div>
 <div class="list" onclick="Xuan(this)" onmouseover="Bian(this)" onmouseout="Hui()">上海</div>
 </div>
  
 <!-- <div id="l" style="width:100%; height:200px; background-color:red;"></div>
  <div id="caidan" style="width:100%; height:100px; background-color:blue;"></div>
  <div style="width:100%; height:1000px; background-color:#000;"></div>
  <input type="button" value="an" οnclick="Hui()" />-->
  
 </body>
 <script type="text/javascript">
 function Xuan(a)
 {
 var list = document.getElementsByClassName("list");
 //清一下(回复原样式)
 for(var i=0;i<list.length;i++)
 {
 list[i].style.backgroundColor = "#03C";
 list[i].removeAttribute("as");
 }
 //选择变色
 a.style.backgroundColor = "red";
 a.setAttribute("as",1);
 }
 function Bian(a)
 {
 var list = document.getElementsByClassName("list");
 //清一下(回复原样式)
 for(var i=0;i<list.length;i++)
 {
 if(list[i].getAttribute("as")!=1)
 {
 list[i].style.backgroundColor = "#03C";
 }
  
 }
 //选择变色
 a.style.backgroundColor = "red";
 }
 function Hui()
 {
 var list = document.getElementsByClassName("list");
 //清一下(回复原样式)
 for(var i=0;i<list.length;i++)
 {
 if(list[i].getAttribute("as")!=1)
 {
 list[i].style.backgroundColor = "#03C";
 }
  
 }
 }
  
 /*function Hui()
 {
 window.scrollTo(0,200);
 }*/
  
 /*window.onscroll = function()
 {
 var c = document.getElementById("caidan");
 //var ch = c.style.height;
 var l = document.getElementById("l");
 var lh = l.style.height;
 //parseInt(ch);
 if(window.scrollY >= parseInt(lh))
 {
 //超出的范围操作
 c.style.position = "fixed";
 c.style.top = "0px";
 }
 else
 {
 //没有超出的范围操作
 c.style.position = "relative";
 }*/
 }
 </script>
 </html>

转载于:https://www.cnblogs.com/F9801/p/8845840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值