全国统一服务热线:400-633-9193

javascript实现电脑和手机版样式切换

    网络     2017-11-20    1319

本文实例为大家分享了javascript实现电脑和手机版样式切换的具体代码,供大家参考,具体内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
      ul{
        list-style: none;
      }
        
    </style>
  </head>
  <body>
    <ul>
      <li>首页</li>
      <li>公司概况</li>
      <li>产品介绍</li>
      <li>成功案例</li>
      <li>合作伙伴</li>
    </ul>
    <div>     
      <button onclick="addStyle()">添加样式效果</button>
      <button onclick="showStyle('pc')">电脑版</button>
      <button onclick="showStyle('mobile')">手机版</button>
    </div>
    <script>
      function addStyle()
      {
        //根据元素的标记名获取元素
        var lis = document.getElementsByTagName('li');
        for(var i = 0;i<lis.length;i++)
        {
          lis[i].style.width = '150px';
          lis[i].style.height= '30px';
          lis[i].style.padding = '5px 10px';
          lis[i].style.marginTop = '1px';
          lis[i].style.backgroundColor = 'rgb(51,51,51)';
          lis[i].style.textAlign = 'center';
          lis[i].style.lineHeight = '30px';
          lis[i].style.color='#fff';
        }
      }
        
      function showStyle(type)
      {
        var lis = document.getElementsByTagName('li');
        for(var i = 0;i<lis.length;i++){
          if(type == 'pc'){
            //PC版
            lis[i].style.float = 'left';//左浮动
            //移除指定的属性
            lis[i].style.removeProperty('clear');
            lis[i].style.width='150px';
          }else{
            //手机版
            lis[i].style.clear = 'both';//清除浮动
            lis[i].style.width='100%';
          }
        }
        
      }
    </script>
  </body>
</html>


  分享到:  
0.2660s