简体   繁体   English

Javascript显示隐藏css / html / js问题

[英]Javascript show hide css/html/js problem

Some code when I call a showhide method in html is not working correctly. 当我在html中调用showhide方法时,某些代码无法正常工作。 The method shows then hides some html content on this webpage, however, the html or css is not functioning correctly. 该方法显示然后在此网页上隐藏了一些html内容,但是html或css无法正常运行。 For example, when the page is loaded in a browser, the space where the div will be shown is just empty space, when there shouldn't be space at all, but when the div is shown it just fills that space. 例如,当页面加载到浏览器中时,将显示div的空间仅为空白空间,根本不应该有空间,但是显示div的空间仅填充了该空间。 The I think it could be something to do with the css, however I am not to sure. 我认为这可能与css有关,但是我不确定。 Here is the CSS id I am using to show and hide. 这是我用来显示和隐藏的CSS ID。

#showAndHide {
    text-align: justify;
    height: auto;
    width: 700px;
    margin: auto;
    position: relative;
    padding-top: 10px;
    padding-bottom: 10px;
    padding-left: 20px;
    padding-right: 10px;
    visibility: hidden;
}

and here is a small sample of the html that this is being applied to. 这是正在应用的html的一个小示例。

<div id="showAndHide">
   <div id="physicalAdress">                        
     <div class="h4">
       <h4> What is your physical address? </h4>
     </div> 

     <p> Property name (if you have one) </p>
     <input type="text" name="propertyName" /><br/>

that html is within the showandhide div, then within a user input div which is: 该html在showandhide div中,然后在用户输入div中,该div是:

.userinput {
    text-align: justify;
    height: auto;
    width: 700px;
    margin: auto;
    position: relative;
    border-collapse: collapse;
    padding-top: 10px;
    padding-bottom: 10px;
    padding-left: 20px;
    padding-right: 10px;
    border-right-style: solid;
    border-left-style: solid;
}

here is the Javascript method. 这是Javascript方法。

function showHideDiv()
{
    var divstyle = new String();
    divstyle = document.getElementById("showAndHide").style.visibility;

    if(document.getElementById("yesPrint").checked == true) 
    {
       document.getElementById("showAndHide").style.visibility = "visible";
    }
    if(document.getElementById("noPrint").checked == true) 
    {
        document.getElementById("showAndHide").style.visibility = "hidden";
    }   
 }

Basically when I run the page, and show the content the styling is becoming skewed and the positioning of the html that is not with the show and hide content is also becoming skewed. 基本上,当我运行页面并显示内容时,样式会倾斜,并且与显示和隐藏内容不同的html的位置也会偏移。

Any ideas/help would be appreciated. 任何想法/帮助将不胜感激。

use display:none instead of visibility: hidden; 使用display:none代替visibility: hidden; .

visibility: hidden; reserves space for element 为元素保留空间

Change visibility:hidden to display:none to prevent the element from taking up space when hidden. 更改visibility:hiddendisplay:none以防止元素在隐藏时占用空间。

You may need to change your JavaScript slightly, but I can't say for sure as you haven't posted it, but you're going to need something like this: 您可能需要稍微更改JavaScript,但是由于您尚未发布JavaScript,因此我不能肯定地说,但是您将需要以下内容:

element.style.display = "block"; //Show the element
function toggle_show(id) {
    var el = document.getElementById(id);
    if (el && el.style) {
        el.style.display = el.style.display == 'none' ? 'block' : 'none';
    }
}

http://webdesign.about.com/od/css/f/blfaqhidden.htm Quoting webdesign: http://webdesign.about.com/od/css/f/blfaqhidden.htm引用网页设计:

"visibility: hidden hides the element, but it still takes up space in the layout. “可见性:隐藏隐藏元素,但是它仍然占据布局中的空间。

display: none removes the element completely from the document. 显示:无将元素从文档中完全删除。 It does not take up any space, even though the HTML for it is still in the source code." 即使它的HTML仍在源代码中,它也不会占用任何空间。”

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

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