我的html页面上有两个div标签(Div1,Div2),标签很少,而且我想在页面加载时隐藏div2标签,并通过隐藏div1标签在单击其他标签时显示它。
谁能帮我使用DOM或javascript如何做到这一点




function myFunction() {
  alert("helloworld");
  document.getElementById("div2").style.display = "none";
}

function myFunction1() {
  alert("helloworld");
  document.getElementById("div1").style.display = "none";
  document.getElementById("div2").style.visibility = "visible";

}


function myFunctionone() {
  alert("helloworld");
  document.getElementById("div1").style.display = "none";
  document.getElementById("div2").style.visibility = "visible";

}

div.round1 {
  border: 1px solid deepskyblue;
  border-radius: 4px;
  height: 170px;
  width: 30%;
  margin: auto;
}

.up {
  vertical-align: -145px;
}

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body onload="myFunction()">

  <div id="div1" class="round1">

    <table>
      <tr>
        <td>
          <img src="html5.gif" alt="Mountain1" style="width:128px;height:128px;">
          <br>If a browser cannot find an image, it will display the alternate text
        </td>

        <td>
          <img class="up" src="pic_mountain.jpg" style="width:138px;height:70px;"> </td>
      </tr>

    </table>
  </div>

  <div id="div2" class="round1">

    <table>
      <tr>
        <td>
          <img src="pic_mountain.jpg" alt="Mountain1" style="width:128px;height:128px;">
          <br>If a browser cannot find an image, it will display the alternate text
        </td>

        <td>
          <img class="up" src="html5.gif" style="width:138px;height:70px;"> </td>
      </tr>

    </table>
  </div>

  <br>
  <div align="left">
    <div class="container">
      <ul class="nav nav-tabs">
        <li class="active"><a data-toggle="tab" href="#home" onclick="myFunctionone()">One</a></li>
        <li><a data-toggle="tab" href="#menu1" onclick="myFunction1()">Two</a></li>
        <li><a data-toggle="tab" href="#menu1">Functional</a></li>
        <li><a data-toggle="tab" href="#menu3">Three</a></li>
      </ul>

      <div class="tab-content">
        <div id="home" class="tab-pane fade in active">
          <h3>One</h3>
          <p>If a browser cannot find an image, it will display the alternate textIf a browser cannot find an image, it will display the alternate text.</p>
        </div>
        <div id="menu1" class="tab-pane fade">
          <h3>Two</h3>
          <p>If a browser cannot find an image, it will display the alternate textIf a browser cannot find an image, it will display the alternate text</p>
        </div>
        <div id="menu2" class="tab-pane fade">
          <h3>Three</h3>
          <p>If a browser cannot find an image, it will display the alternate textIf a browser cannot find an image, it will display the alternate text</p>
        </div>
        <div id="menu3" class="tab-pane fade">
          <h3>Others</h3>
          <p>If a browser cannot find an image, it will display the alternate textIf a browser cannot find an image, it will display the alternate text</p>
        </div>
      </div>
    </div>
  </div>



</body>

</html>

最佳答案

$(document).ready(function () {
      $('#div2').hide()
      $('.nav a').click(function () {
          $('#div2').show()
          $('#div1').hide()
      })
})


你是这个意思吗

10-08 04:59