简体   繁体   English

使用严格的JavaScript隐藏和显示DIV,且没有嵌入式JS或CSS

[英]Hide and Show DIV using strict javascript with no embedded JS or CSS

I have searched and searched over so many previously answered questions and to my surprise, NONE of them answer what I am trying to accomplish. 我搜索并搜索了许多以前回答过的问题,令我惊讶的是,没有一个回答我要完成的任务。 with JQuery this is easy but I am struggling with strict javascript. 使用JQuery,这很容易,但是我在使用严格的javascript。

I cannot have any embedded JS OR CSS in the html... 我不能在html中嵌入任何JS或CSS ...

This is what I have so far: 这是我到目前为止的内容:

function showhide()
{
    var billingaddress = document.getElementById("billingaddress");
    if (billingaddress.style.display === "block")
        {
            billingaddress.style.display = "none";
        }
    else if (billingaddress.style.display === "none")
        {
            billingaddress.style.display = "block";
        }
}

function init () 
{   
    var billingcheckbox = document.getElementById("billing");

    if (billingcheckbox.checked)
        {
            showhide();
        } 
    else 
        {
            showhide();
        } 

It is hidden by default using CSS. 默认情况下,使用CSS将其隐藏。

Cheers, 干杯,

With he code you've provided, it can be done like this. 使用您提供的代码,可以像这样完成。

billingaddress.style.display is empty by default, you can easily check it in the if without a comparison. billingaddress.style.display默认为空,如果没有比较,您可以轻松地检查它。

 function showhide() { if (billingaddress.style.display) billingaddress.style.display = "" else billingaddress.style.display = 'none'; } var billingaddress = document.getElementById("billingaddress") var billingcheckbox = document.getElementById("billing") billingcheckbox.addEventListener('change', showhide) billingcheckbox.checked = true showhide() 
 <input type="checkbox" id="billing"/> Hide <div id="billingaddress">Lorem Ipsum</div> 

It's as easy as this: 就这么简单:

this keyword inside event handler references checkbox element so you can get checkbox state with this.checked property. 事件处理程序中的this关键字引用checkbox元素,因此您可以使用this.checked属性获取复选框状态。

 <input type="checkbox" id="billing"> <input type="text" id="billingaddress" placeholder="Address" style="display:none"> <script> var billingAddress = document.getElementById("billingaddress"); var billingCheckbox = document.getElementById("billing"); billingCheckbox.addEventListener('click', function(event) { billingAddress.style.display = this.checked ? "block" : "none"; }) </script> 

The code below should do it -- but to be sure make sure all your IDs are matching up with what is in the HTML markup: 下面的代码应该执行此操作-但要确保所有ID与HTML标记中的匹配:

function showhide(show) {   

    var billingaddress = document.getElementById("billingaddress"); 

    if (show) { 

        billingaddress.style.display = "block"; 

    } else {    

        billingaddress.style.display = "none";
    }
}   

function init () {   

    var billingcheckbox = document.getElementById("billing");   

    if (billingcheckbox.checked) {  

        showhide(true); 

    } else {    

        showhide(false);
    }
}

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

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