简体   繁体   English

如何使用JavaScript隐藏/显示div标签?

[英]How to hide/show div tags using JavaScript?

Basically, I'm trying to make a link that, when pressed, will hide the current body div tag and show another one in its place, unfortunately, when I click the link, the first body div tag still appears. 基本上,我正在尝试创建一个链接,当按下时,将隐藏当前的body div标签并在其位置显示另一个,不幸的是,当我单击该链接时,仍会出现第一个body div标记。 Here is the HTML code: 这是HTML代码:

<div id="body">
    <h1>Numbers</h1>
</div>
<div id="body1">
    Body 1
</div>

Here is the CSS code: 这是CSS代码:

#body {
    height: 500px;
    width: 100%;
    margin: auto auto;
    border: solid medium thick;
}

#body1 {
    height: 500px;
    width: 100%;
    margin: auto auto;
    border: solid medium thick;
    display: hidden;
}

Here is the JavaScript code: 这是JavaScript代码:

function changeDiv() {
  document.getElementById('body').style.display = "hidden"; // hide body div tag
  document.getElementById('body1').style.display = "block"; // show body1 div tag
  document.getElementById('body1').innerHTML = "If you can see this, JavaScript function worked"; // display text if JavaScript worked
}

NB: CSS tags are declared in different files 注意:CSS标签在不同的文件中声明

Have you tried 你有没有尝试过

document.getElementById('body').style.display = "none";

instead of 代替

document.getElementById('body').style.display = "hidden"; ?

just use a jquery event listner , click event. 只需使用jquery事件列表器,单击事件。 let the class of the link is lb... i am considering body as a div as you said... 让链接的类是lb ...我正在考虑身体作为div你说...

$('.lb').click(function() {
    $('#body1').show();
    $('#body').hide();
 });

Use the following code: 使用以下代码:

function hide {
document.getElementById('div').style.display = "none";
}
function show {
document.getElementById('div').style.display = "block";
}

Consider using jQuery . 考虑使用jQuery Life is much easier with: 生活更容易:

$('body').hide(); $('body1').show();

You can Hide/Show Div using Js function. 您可以使用Js功能隐藏/显示Div。 sample below 以下样本

   <script>
        function showDivAttid(){

            if(Your Condition)
            {
             document.getElementById("attid").style.display = 'inline';
            }
            else
            {
            document.getElementById("attid").style.display = 'none';
            }

        }

    </script>

HTML - Show/Hide this text HTML - 显示/隐藏此文本

Set your HTML as 将您的HTML设置为

 <div id="body" hidden="">
 <h1>Numbers</h1>
 </div>
 <div id="body1" hidden="hidden">
 Body 1
 </div>

And now set the javascript as 现在将javascript设置为

     function changeDiv()
      {
      document.getElementById('body').hidden = "hidden"; // hide body div tag
      document.getElementById('body1').hidden = ""; // show body1 div tag
      document.getElementById('body1').innerHTML = "If you can see this, JavaScript function worked"; 
      // display text if JavaScript worked
       }

Check, it works. 检查,它的工作原理。

尝试你写document.getElementById('id')。style.visibility =“hidden”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM