Javascript 在将值传递给 js 变量后显示一个具有 style="display:none" 的 div

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

show a div having style="display:none" after passing value to js variable

javascripthtmljsp

提问by sam

Initially the div id =" showHide" is invisible. I want to make it visible when the value of vall is not null. But the code is not working. Though variable vall is getting values, the div is still not visible. What is the problem with js codescript?? I am using jsp.

最初 div id =" showHide" 是不可见的。我想让它在val的值不为 null时可见。但是代码不起作用。尽管变量 val 正在获取值,但 div 仍然不可见。js代码脚本有什么问题??我正在使用jsp。

<head>
</head>

<body>
<div id =" showHide" style="display:none">

    /*HTML TABLE*/
</div>

<script type="text/javascript">

 alert("first alert");
 var vall = "<%=vall%>";    

    if (vall != null)
        {            
            var mydiv = getElementById.("showHide");
            mydiv.style.display ="block";
            alert("second alert");
            <% System.out.println("jsvar="+vall);%>

             }

    </script>
</body>

When I logged in the console i got the error:

当我登录控制台时,出现错误:

uncaught syntaxError: unexpected token (

未捕获的语法错误:意外标记(

and both the alert box are also not showing.

并且警告框也没有显示。

回答by John Stimac

Try:

尝试:

mydiv.style.display = "block";

instead of

代替

mydiv.style.visibility = "visible";

回答by Andrew Marshall

visibilityand displayare different. Instead you should change the displayvalue from noneto block(the default for a <div>):

visibility并且display是不同的。相反,您应该将display值从noneto block( a 的默认值)更改为<div>

mydiv.style.display = "block";

回答by jjathman

Are you sure the content has actually been loaded before your JS is being called? Try moving your JS to the end of the body or wrap your code in a function that can be called on page load. If you want to be really fancy you should use jQuery to help you out with this. http://api.jquery.com/ready/

你确定在你的 JS 被调用之前内容实际上已经被加载了吗?尝试将您的 JS 移动到正文的末尾或将您的代码包装在一个可以在页面加载时调用的函数中。如果你想变得真正花哨,你应该使用 jQuery 来帮助你解决这个问题。http://api.jquery.com/ready/

回答by KiNG H

I think You forget to declare variable mydiv.

我认为您忘记声明变量 mydiv。

replace this line mydiv = document.getElementById("showHide");

替换这一行 mydiv = document.getElementById("showHide");

by

经过

var mydiv = document.getElementById("showHide");

var mydiv = document.getElementById("showHide");

enter code here<div id ="showHide" style="display:none">

/*HTML TABLE*/

// javascript code just check this out

// javascript 代码只需检查一下

var vall = 10;
var mydiv = null
if (vall != null)
    {
        mydiv = document.getElementById("showHide");
        mydiv.style.display ="block";

    }

回答by Laughing

<div id =" showHide" style="display:none"> 

/*HTML TABLE*/ 
</div> 

remove the space of id and put the Js code to back of div , try it.

去掉id的空格,把js代码放到div后面,试试看。

回答by me_digvijay

I think when your code in the script tag runs it does not find the div tag with that id, and when the div is rendered in the page you again set its visibility to hidden.

我认为当脚本标记中的代码运行时,它找不到具有该 ID 的 div 标记,并且当在页面中呈现 div 时,您再次将其可见性设置为隐藏。

I suggest to put your script tag after the div.

我建议把你的脚本标签放在 div 之后。

回答by Amita Patil

instead of

代替

 var mydiv = getElementById.("showHide");

use

 var mydiv = document.getElementById.("showHide");