Home > Web Front-end > JS Tutorial > Javascript implements style switching between computer and mobile versions

Javascript implements style switching between computer and mobile versions

小云云
Release: 2017-12-09 13:19:46
Original
1598 people have browsed it

This article mainly introduces JavaScript to implement style switching between computer and mobile versions in detail. It has certain reference value. Interested friends can refer to it. I hope it will be helpful to everyone.

<!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> 
    <p>      
      <button onclick="addStyle()">添加样式效果</button> 
      <button onclick="showStyle(&#39;pc&#39;)">电脑版</button> 
      <button onclick="showStyle(&#39;mobile&#39;)">手机版</button> 
    </p> 
    <script> 
      function addStyle() 
      { 
        //根据元素的标记名获取元素 
        var lis = document.getElementsByTagName(&#39;li&#39;); 
        for(var i = 0;i<lis.length;i++) 
        { 
          lis[i].style.width = &#39;150px&#39;; 
          lis[i].style.height= &#39;30px&#39;; 
          lis[i].style.padding = &#39;5px 10px&#39;; 
          lis[i].style.marginTop = &#39;1px&#39;; 
          lis[i].style.backgroundColor = &#39;rgb(51,51,51)&#39;; 
          lis[i].style.textAlign = &#39;center&#39;; 
          lis[i].style.lineHeight = &#39;30px&#39;; 
          lis[i].style.color=&#39;#fff&#39;; 
        } 
      } 
       
      function showStyle(type) 
      { 
        var lis = document.getElementsByTagName(&#39;li&#39;); 
        for(var i = 0;i<lis.length;i++){ 
          if(type == &#39;pc&#39;){ 
            //PC版 
            lis[i].style.float = &#39;left&#39;;//左浮动 
            //移除指定的属性 
            lis[i].style.removeProperty(&#39;clear&#39;); 
            lis[i].style.width=&#39;150px&#39;; 
          }else{ 
            //手机版 
            lis[i].style.clear = &#39;both&#39;;//清除浮动 
            lis[i].style.width=&#39;100%&#39;; 
          } 
        } 
       
      } 
    </script> 
  </body> 
</html>
Copy after login

Related recommendations:

The latest Javascript programmer interview questions and solution methods

javaScript and jQuery implement automatic loading

JavaScript writes maintainable code

The above is the detailed content of Javascript implements style switching between computer and mobile versions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template