Home >Web Front-end >JS Tutorial >Implementing simple style switching effect code based on JS_javascript skills
The example in this article describes the implementation of a simple style switching effect based on JS. Share it with everyone for your reference. The details are as follows:
This is a simple style switching code based on JS, which can switch CSS styles freely. Many big websites are using it now. I think this is a very practical and good function. I hope everyone likes it.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-simple-tab-cha-style-codes/
The specific code is as follows:
<!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=gb2312" /> <script language="javascript" type="text/javascript"> var lastObj=null; var lastIdx=1; function test(obj,idx){ if(!lastObj){ lastObj = document.getElementById("test"); lastIdx = 1; } lastObj.className = "new"+lastIdx; var old = document.getElementById("list"+lastIdx); if(old)old.style.display="none"; obj.className = "class"+idx; var n = document.getElementById("list"+idx); if(n)n.style.display="block"; lastObj = obj; lastIdx = idx; } </script> <style type="text/css"> .class1{ color:#FF0000} .new1{ color:#996633} .class2{ color:#FF0000} .new2{ color:#996633} </style> <title>JS实现样式切换</title> </head> <body> <a href="#" class="class1" id="test" onclick="test(this,1)">list1</a> <a href="#" class="new2" onclick="test(this,2)">list2</a> <div id="list1"> test1 </div> <div id="list2" style="display:none"> test2 </div> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.