js控制div点击隐藏显示

比眉伴天荒 2022-09-26 15:49 370阅读 0赞
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="author" content="http://www.softwhy.com/"/>
  6. <title>点击切换</title>
  7. <style type="text/css">
  8. #thediv {
  9. width: 200px;
  10. height: 50px;
  11. background: #ccc;
  12. display: none;
  13. }
  14. </style>
  15. <script type="text/javascript">
  16. window.onload = function () {
  17. var obt = document.getElementById("bt");
  18. var odiv = document.getElementById("thediv");
  19. function getStyle(obj, attr) { // 谁的 那个属性
  20. if (obj.currentStyle) // ie 等
  21. {
  22. return obj.currentStyle[attr];
  23. }
  24. else {
  25. return window.getComputedStyle(obj, null)[attr]; // w3c 浏览器
  26. }
  27. }
  28. //console.log(getStyle(odiv, 'display'));
  29. obt.onclick = function () {
  30. //获取的样式需要写在点击事件里面,写在外面只能获取一次,不能动态获取,
  31. if (getStyle(odiv, 'display') == "none") {
  32. odiv.style.display = "block";
  33. obt.value = "隐藏模块";
  34. }
  35. else {
  36. odiv.style.display = "none";
  37. obt.value = "显示模块";
  38. }
  39. }
  40. }
  41. </script>
  42. <body>
  43. <input type="button" id="bt" value="显示模块"/>
  44. <div id="thediv"></div>
  45. </body>
  46. </html>

以上为第一种

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="author" content="http://www.softwhy.com/"/>
  6. <title>点击切换</title>
  7. <style type="text/css">
  8. #thediv {
  9. width: 200px;
  10. height: 50px;
  11. background: #ccc;
  12. display: none;
  13. }
  14. </style>
  15. <script type="text/javascript">
  16. window.onload = function () {
  17. var obt = document.getElementById("bt");
  18. var odiv = document.getElementById("thediv");
  19. obt.onclick = function () {
  20. if (odiv.style.display== "block") {
  21. //第一判断时将none改为block即可避免第一次点击无效的问题
  22. odiv.style.display = "none";
  23. obt.value = "显示模块";
  24. }
  25. else {
  26. odiv.style.display = "block";
  27. obt.value = "隐藏模块";
  28. }
  29. }
  30. }
  31. </script>
  32. <body>
  33. <input type="button" id="bt" value="显示模块"/>
  34. <div id="thediv"></div>
  35. </body>
  36. </html>

以上为第二种

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="author" content="http://www.softwhy.com/" />
  6. <title>蚂蚁部落</title>
  7. <style type="text/css">
  8. #thediv{
  9. width:200px;
  10. height:50px;
  11. background:#ccc;
  12. }
  13. </style>
  14. <script type="text/javascript">
  15. window.οnlοad=function(){
  16. var obt=document.getElementById("bt");
  17. var odiv=document.getElementById("thediv");
  18. obt.οnclick=function(){
  19. if(odiv.style.display=="none"){
  20. odiv.style.display="block";
  21. obt.value="隐藏模块";
  22. }
  23. else{
  24. odiv.style.display="none";
  25. obt.value="显示模块";
  26. }
  27. }
  28. }
  29. </script>
  30. <body>
  31. <input type="button" id="bt" value="显示模块"/>
  32. <div id="thediv" style="display:none"></div>
  33. </body>
  34. </html>

以上为第三种

发表评论

表情:
评论列表 (有 0 条评论,370人围观)

还没有评论,来说两句吧...

相关阅读