javascript 刷新jsp页面上的特定div而不是整个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15915817/
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
refresh a specific div on jsp page and not whole page
提问by user2137186
i want to refresh a div on the basis of input ...here is a sample code:
我想在输入的基础上刷新一个 div ...这是一个示例代码:
<%@ page language="java" contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
</head>
<body>
<input type="button" id="1" value="1" onClick="javaScript:update('1');"/>
<input type="button" id="2" value="2" onClick="javaScript:update('2');"/>
<%
int value=1;
%>
<div id="refresh">
<%=value %>
</div>
<script type="text/javascript">
function update(input)
{
value=input;
alert(value);
}
</script>
</body>
</html>
so here is what i want when page loads it will print 1 as value is initialized with 1 but if i click 2 i want that div to print 2 whose value gets updated in update function of java script ...i don't want to refresh whole page just want to refresh that div ...i don't want to use JQUERY.load as it loads the whole page in div i just want to refresh that div
所以这就是我想要的页面加载时它会打印 1 因为值被初始化为 1 但如果我点击 2 我希望那个 div 打印 2 其值在 java 脚本的更新函数中更新......我不想刷新整个页面只是想刷新那个 div ...我不想使用 JQUERY.load 因为它在 div 中加载整个页面我只想刷新那个 div
回答by Ben McCormick
function update(input)
{
$("#refresh").html(input)
}
That should be all you need
这应该就是你所需要的
.html()
is the jQuery function that changes the innerHTML of an element.
.html()
是改变元素的innerHTML 的jQuery 函数。
For ajax you can look at the jquery docs hereand this is a nice looking example for jQUery + php
对于 ajax,您可以在此处查看 jquery 文档,这是 jQUEry + php 的一个漂亮示例