我在ajax的回调函数里改变了div的颜色
但是每次刷新页面该div又变回原来的了
怎么做能让他不变回来?
解决方案 »
- jsp中图片显示红叉
- 实在整不出来了,大家给点思路,有两个List<ListOrderedMap>比较的问题
- 求救:如果不知道列名,在JSP页面中怎样写?
- 财务系统的凭证这个模块是做什么用的啊~
- MVC模式(JSP+JavaBean+Servlet)JSP分页代码!
- jsp中如何调用指定的dll中的函数?望多多指教!
- 本人在读取mysql数据库时抛出NullPointerException异常,请高手解答?
- 关于用jspsmart类上载问题的问题
- jsp里如何調上面的函數呢,我這樣寫有錯的,如何寫才正確呢???
- 如何配置Tomcat?
- 急:sql高级查询出错
- 求解:Servlet与JSP传递的参数是对象
for example
<div style = "color:red"/>
别的用户看不到了呀
保存在缓存中,如数据库、文件、cookie、application中
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<select onChange="a(this.value)">
<option value="1">紅</option>
<option value="2">黃</option>
<option value="3">綠</option>
<option value="4">藍</option>
</select>
<div id="ajax"></div>
<script>
function a(o){
var url="2.jsp?op="+o;
var xmlHttpRequest=new ActiveXObject("microsoft.xmlhttp");
xmlHttpRequest.open("post",url,true);
xmlHttpRequest.send(null);
xmlHttpRequest.onreadystatechange=function(){
if(xmlHttpRequest.readystate==4 && xmlHttpRequest.status==200){
document.getElementById("ajax").innerHTML=xmlHttpRequest.responseText;
}
}
}
</script>
</body>
</html>2.jsp<%
String op=request.getParameter("op");
String red="<div style='background-color:#FF0000; width:200px; height:200px '></div>";
String yellow="<div style='background-color:#CCFF00; width:200px; height:200px '></div>";
String green="<div style='background-color:#00CC00; width:200px; height:200px'></div>";
String blue="<div style='background-color:#0066FF; width:200px; height:200px '></div>";
if(op.equals("1")){
out.println(red);
}else if(op.equals("2")){
out.println(yellow);
}else if(op.equals("3")){
out.println(green);
}else if(op.equals("4")){
out.println(blue);
}
%>
你把1.JSP里的<div id="ajax"></div>改成<div id="ajax" style="background-color:#cccccc; width:200px; height:200px"></div>這樣默認是灰色了,你可以在下拉里選擇你要的顏色 絕對不會變回灰色。如果你呀的需求是 我選了 紅色 每次刷新還是 紅色的話 那用COOKIE吧 在1.JSP里選擇好了顏色 后 后臺2.JSP里 你把選擇的顏色 放到COOKIES里去每次刷新頁面的時候 把COOKIE讀出來 用 <% out.print(...) %>放在 <div id="ajax" style="... <%out.println(); %> ... " 這里除非你手動刷新了頁面 。
我發現 COOKIE里 無發保存 有特殊符號的 字符串 如 < > # 現在完工了一共2個頁面 1.JSP 和 2.JSP<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<select onChange="a(this.value)">
<option value="1">紅</option>
<option value="2">黃</option>
<option value="3">綠</option>
<option value="4">藍</option>
</select>
<%
String c="";
Cookie[]cookies=request.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equalsIgnoreCase("colour")){
c=cookies[i].getValue();
out.println(c);
if("red".equals(c)){
out.println("<div style='background-color:#FF0000; width:200px; height:200px'></div>");
}else if("yellow".equals(c)){
out.println("<div style='background-color:#CCFF00; width:200px; height:200px'></div>");
}else if("green".equals(c)){
out.println("<div style='background-color:#00CC00; width:200px; height:200px'></div>");
}else if("blue".equals(c)){
out.println("<div style='background-color:#0066FF; width:200px; height:200px'></div>");
}
}
}
}else{
out.println("<div id='ajax' style='width:200px;height:200px'></div>");
}
%>
<script>
function a(o){
var url="2.jsp?op="+o;
var xmlHttpRequest=new ActiveXObject("microsoft.xmlhttp");
xmlHttpRequest.open("post",url,true);
xmlHttpRequest.send(null);
xmlHttpRequest.onreadystatechange=function(){
if(xmlHttpRequest.readystate==4 && xmlHttpRequest.status==200){
var c=xmlHttpRequest.responseText.replace(/(^\s*)|(\s*$)/g,"");
if(c=="red"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else if(c="yellow"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else if(c=="blue"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else if(c=="green"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else{
alert("没有颜色!");
}
}
}
}
</script>
</body>
</html>
<%
String op=request.getParameter("op");
if(op.equals("1")){
Cookie cookie=new Cookie("colour","red");
response.addCookie(cookie);
out.println("#FF0000");
}else if(op.equals("2")){
Cookie cookie=new Cookie("colour","yellow");
response.addCookie(cookie);
out.println("#CCFF00");
}else if(op.equals("3")){
Cookie cookie=new Cookie("colour","green");
response.addCookie(cookie);
out.println("#00CC00");
}else if(op.equals("4")){
Cookie cookie=new Cookie("colour","blue");
response.addCookie(cookie);
out.println("#0066FF");
}
%>
1.JSP 頁面 選擇顏色 不刷新 變換 ,刷新1.JSP頁面 保持 最后一次選擇的顏色 。PS:給分吧。。給了別人也沒關系,有啥問題請講。
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<select onChange="a(this.value)">
<option value="1">紅</option>
<option value="2">黃</option>
<option value="3">綠</option>
<option value="4">藍</option>
</select>
<%
String c="";
Cookie[]cookies=request.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equalsIgnoreCase("colour")){
c=cookies[i].getValue();
out.println(c);
if("red".equals(c)){
out.println("<div id='ajax' style='background-color:#FF0000; width:200px; height:200px'></div>");
}else if("yellow".equals(c)){
out.println("<div id='ajax' style='background-color:#CCFF00; width:200px; height:200px'></div>");
}else if("green".equals(c)){
out.println("<div id='ajax' style='background-color:#00CC00; width:200px; height:200px'></div>");
}else if("blue".equals(c)){
out.println("<div id='ajax' style='background-color:#0066FF; width:200px; height:200px'></div>");
}
}
}
}else{
out.println("<div id='ajax' style='width:200px;height:200px'></div>");
}
%>
<script>
function a(o){
var url="2.jsp?op="+o;
var xmlHttpRequest=new ActiveXObject("microsoft.xmlhttp");
xmlHttpRequest.open("post",url,true);
xmlHttpRequest.send(null);
xmlHttpRequest.onreadystatechange=function(){
if(xmlHttpRequest.readystate==4 && xmlHttpRequest.status==200){
var c=xmlHttpRequest.responseText.replace(/(^\s*)|(\s*$)/g,"");
if(c=="red"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else if(c="yellow"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else if(c=="blue"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else if(c=="green"){
document.getElementById("ajax").style.background=xmlHttpRequest.responseText;
}else{
alert("没有颜色!");
}
}
}
}
</script>
</body>
</html>
ajax不是没意义了
看清楚需求 選擇好 顏色(假如:紅 藍 綠 黃) 即 顯示 顏色 (AJAX)當你最后選擇了 綠色手動刷新 頁面還是綠色即顏色保存的效果沒用到SESSION 沒用到數據庫 用到COOKIE這點操作就要依靠服務器保存之類的都不是好辦法。
其他的用户的界面同样没有啊我在服务器端设置了一个变量,每次加载都访问服务器
不喜歡COOKIES 可以放SESSION ,再說COOKIE里我就放了 red yellow blue green
當然這些名字你可以直接取。