javascript 我如何在 asp.net mvc 中隐藏 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30435809/
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-28 12:10:23  来源:igfitidea点击:

How do i hide div in asp.net mvc

javascriptasp.netasp.net-mvc

提问by abcd

I have 3 div which are:

我有 3 个 div,它们是:

<div class="widgetbox" id="divone">Content 1</div>

<div class="widgetbox" id="divone">Content 1</div>

<div class="widgetbox" id="divtwo">Content 2</div>

<div class="widgetbox" id="divtwo">Content 2</div>

<div class="widgetbox" id="divthree">Content 3</div>

<div class="widgetbox" id="divthree">Content 3</div>

I need to hide two of these div so that I can make a condition to decide which to appear and which to hide later on. I tried to hide it like below, but it doesn't work. I think I'm using mvc 4. sorry for bad english.

我需要隐藏其中的两个 div,以便我可以创建一个条件来决定稍后显示哪些以及隐藏哪些。我试图像下面那样隐藏它,但它不起作用。我想我正在使用 mvc 4。抱歉英语不好。

<script type='text/javascript'>
    $("#divtwo").hide();
    $("#divthree").hide();    
</script>

回答by Jaimin Soni

Here you get two answers:

在这里你会得到两个答案:

1)

1)

<script type='text/javascript'>
    $(document).ready(function(){
        $("#divtwo").hide();
        $("#divthree").hide();
    });
</script>

2) try this one. No need any references.

2)试试这个。不需要任何参考。

<script type='text/javascript'>
    window.onload=function(){
        document.getElementById("divtwo").style.display='none';
        document.getElementById("divthree").style.display='none';
    }
</script>

回答by Rahul Nikate

First you need to add jQuery reference. Here you is way to get latest jQuery in your project

首先,您需要添加 jQuery 引用。这是在您的项目中获取最新 jQuery 的方法

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

then your code will work for .hide()

那么您的代码将适用于 .hide()

<script type='text/javascript'>
    $("#divtwo").hide();
    $("#divthree").hide();    
</script>