javascript 如何使用 Java 在 JSP 中异步显示/隐藏 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12393928/
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
How can I asynchronously show/hide div in JSP using Java
提问by Brian
I want to programmatically show a div tag after some processing has completed while rendering a JSP. What's the best way to do this using Java? Using jQuery I would do this:
我想在渲染 JSP 时完成一些处理后以编程方式显示 div 标记。使用 Java 执行此操作的最佳方法是什么?使用 jQuery 我会这样做:
$('#mydiv').removeClass("hide_me");
...or...
...或者...
$('#mydiv').show();
How can I do this programmatically in Java while rendering the page?
渲染页面时如何在 Java 中以编程方式执行此操作?
回答by Affe
Assuming you have the standard JSP setup including JSTL and have mapped it to 'c' you could just do:
假设您拥有包括 JSTL 在内的标准 JSP 设置并将其映射到“c”,您可以执行以下操作:
<c:if test="${myCondition}">
<div id="mDiv">
content
</div>
</c:if>
It does seem from the comments like there is some confusion about rendering JSP on the server vs rendering content in the browser. Everything that happens in the JSP is server side work that has to completely finish before the browser receives the generated document and starts drawing it. You can't use JSP to change content that is already on the user's screen. You need javascript, html5, etc, for that.
从评论看来,在服务器上呈现 JSP 与在浏览器中呈现内容存在一些混淆。JSP 中发生的一切都是服务器端工作,必须在浏览器接收生成的文档并开始绘制它之前完全完成。您不能使用 JSP 来更改用户屏幕上已经存在的内容。为此,您需要 javascript、html5 等。
回答by topchef
With JSP Java runs on the server (unlike JavaScript that runs within browser) so conditionally render your <DIV>
using Java if statement within JSP:
使用 JSP Java 在服务器上运行(与在浏览器中运行的 JavaScript 不同),因此有条件地呈现您<DIV>
在 JSP 中使用 Java 的 if 语句:
<% if( test="true" ) { %>
<DIV>....</DIV>
<% } %>
回答by Jasper de Vries
I think you are looking for something like this:
我想你正在寻找这样的东西:
<div id="loader">Loading / GIF animation</div>
<div id="result" style="display:none;">
Lots of data.
Should be flushed to the browser every now and then.
This will take seconds...
</div>
<script type="text/javascript">
$("#loader").hide();
$("#result").show();
</script>