javascript 在加载时显示/隐藏 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17466277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:26:38  来源:igfitidea点击:

Showing/hiding div onload

javascriptjqueryhtmlhideshow

提问by Jo?o C

So I have a class of div's that i want to be shown or hidden depending on a variable i get.

所以我有一类 div,我想根据我得到的变量显示或隐藏。

I have this code:

我有这个代码:

**HTML**

<div class="div_estado" >

    testing that
</div>

<div class="div_estado" >

    testing this
</div>

**JS**

$(document).ready(function(){
var estado= 4; //instead of 4 i have "<?php echo $_GET['estado']; ?>" but let's supose its' value is 4
        if (parseInt(estado)==5) 
        {                       
            $('.div_estado').show; // not working
        }
        else {
            $('.div_estado').hide; // not working
        }
});

However it doesn't work properly, can you help me solve this?

但是它不能正常工作,你能帮我解决这个问题吗?

DEMO

演示

回答by c.P.u1

hideand showare methods, not properties. The syntax for invoking a JavaScript function requires a set of parenthesesafter the function name.

hideshow是方法,而不是属性。调用 JavaScript 函数的语法要求在函数名称后有一组括号

$('.div_estado').show();

$('.div_estado').hide();

Updated JSFiddle

更新了 JSFiddle

回答by Sergio

FIDDLE

小提琴

You should use "show()" and not just "show". Check the jQuery API info here.

您应该使用“show()”而不仅仅是“show”。在此处检查 jQuery API 信息。

$(document).ready(function(){
var estado= 4; //instead of 4 i have "<?php echo $_GET['estado']; ?>" but let's supose its' value is 4
        if (parseInt(estado)==5) 
        {                       
            $('.div_estado').show(); // not working
        }
        else {
            $('.div_estado').hide(); // not working
        }
});

回答by mohkhan

You are forgetting (). It is a function not a property. Please change it to...

你忘记了()。它是一个函数而不是一个属性。请改成...

$('.div_estado').show();

...

$('.div_estado').hide();

回答by Magness

You are simply missing brackets, it is show()and hide().

您只是缺少括号,它是show()and hide()

回答by Abhishek Motiwale

Use CSSto hide div

使用 CSS隐藏 div

div.radio {
    display:none;
}

IN JSShow div on load

IN JS在加载时显示 div

$(document).ready(function () {
    $('div_estado').show();
});