JavaScript is a literal scripting language. It is a dynamically typed, weakly typed, prototype-based language with built-in support for types. Its interpreter is called the JavaScript engine, which is part of the browser and is widely used in client-side scripting languages. It was first used on HTML (an application under Standard Universal Markup Language) web pages to add dynamic functions to HTML web pages. . The example in this article shares with you the specific code for switching between computer and mobile version styles in JavaScript for your reference. The specific content is as follows.
<!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>
The above content is a tutorial on how to implement the style switching function between computer and mobile versions in JavaScript. I hope it can help everyone.
Related recommendations:
Basic syntax of JavaScript study notes
Implementing simple style switching effect code based on JS_javascript skills
The above is the detailed content of How to implement the style switching function between computer and mobile versions in javascript. For more information, please follow other related articles on the PHP Chinese website!