簡體   English   中英

使用錨點在 asp mvc 應用程序中顯示/隱藏 div

[英]Using an anchor to show/hide divs in an asp mvc app

在我的 Asp MVC 程序中,我可以用一個按鈕來切換一個 div。 cshtml:

<button onclick="ShowPubs()"> Click to show or hide</button>

腳本:

函數 ShowPubs() {

var x = document.getElementById("myPubs");

if (x.style.display === "none") {

    x.style.display = "block";

} else {

    x.style.display = "none";

}

}

這工作正常,

但是,當嘗試使用此代碼中的鏈接時:cshtnl:

<div id="AboutShow" style="display:block">
   Show the hidden piece <a href="#" onclick="ShowAbout();">Show &#9660;</a>

  </div>

  <div id="AboutHide" style="display:none">
   Hide these details <a href="#" onclick="ShowAbout();">Hide &#9650;</a>

   A lot more stuff

  </div>

使用這個 JavaScript:

函數 ShowAbout() {

var x = document.getElementById("AboutShow");

var y = document.getElementsById("AbourHide");

if (x.style.display === "none") {

    x.style.display = "block";

    y.style.display = "none";

} else {

    x.style.display = "none";

    y.style.display = "b;pck";

}

return false;

}

頁面 url 將 # 添加到 url 並且沒有其他任何事情發生,請問我在這里做錯了什么?

在其他情況下,您使用y.style.display="b;pck"這不是正確的方式 它必須是block

你需要這樣的東西

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> #myDIV { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; } </style> </head> <body> <a href="#" onclick="myFunction()">Try it</a> <div id="myDIV"> This is my DIV element. </div> <script> function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } </script> </body> </html>

讓我知道這是否適合您

  1. getElementsById更改為getElementById
  2. AbourHide更改為AboutHide
  3. 更改b;pckblock

代碼:

function ShowAbout() {

            var x = document.getElementById("AboutShow");

            var y = document.getElementById("AboutHide");

            if (x.style.display === "none") {

                x.style.display = "block";

                y.style.display = "none";

            } else {

                x.style.display = "none";

                y.style.display = "block";

            }

            return false;
        }

結果: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM