由于老师不让把项目源代码发上来,所以只好花点时间重写了一下。
发现这个BUG大概是这样的,编写网站时我一向用Chrome浏览器来调试前台,所以一直都没发现问题。那天发现IE上有乱码,我用火狐试了一下,没问题,可见是IE的bug了。话不多说,先上代码:
<!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=utf-8">
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title></title>
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/public/jqGrid/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
submit();
$('#xz').change(submit);
});
function submit()
{
var t=$("#xz").val();
var tc;
if(t=="1")
{
tc="第一页";
}
else if(t=="2")
{
tc="第二页";
}
else if(t=="3")
{
tc="第三页";
}
var src="forBhbwh?dto.tc="+tc;
$("#frm").attr("src",src);
}
</script>
</head>
<body bgcolor="#FFFFFF">
<select id="xz">
<option id="1" value="1">1</option>
<option id="2" value="2">2</option>
<option id="3" value="3">3</option>
</select>
<br/>
<iframe id="frm" src="#" width="820" height="352" frameborder="0" scrolling="auto"></iframe>
</body>
</html>
以下是iframe子页面的代码
<!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=utf-8">
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
</head>
<body bgcolor="#FFFFFF">
<div id="name"><s:property value='dto.tc'></s:property></div>
</body>
</html>
至于后台和struts2配置文件的相关代码我就不发了吧,和这没关系,而且相信大家仅仅看前台也看得懂。
这段代码的意思就是,在父页面选择相应的数字,iframe子页面中就显示相应的中文:第N页。效果如下:
当然,以上是chrome和火狐的效果。而IE(仅试过IE8和IE9)就不同了,如下:
而只有一开始会这样,如果此时选择下拉框2,即使是IE也不会有乱码
然后用IE再次选择1,没乱码。不上图了,否则大家也会怀疑我用chrome截的那张图冒充是IE浏览器的。
只有一开始才会这样,而且我跳进submit函数调试也发现没问题,如图:
明显是bug了。
解决方案:把一开始iframe里面的src地址直接写上forBhbwh?dto.tc=“第一页",代码:
<iframe id="frm" src="forBhbwh?dto.tc=第一页" width="820" height="352" frameborder="0" scrolling="auto"></iframe>
乱码问题解决。还是上图吧:
如果一开始就在iframe中给出地址,那么document.ready的函数中就没必要执行一次submit函数了。代码:
<!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=utf-8">
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title></title>
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/public/jqGrid/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#xz').change(submit);
});
function submit()
{
var t=$("#xz").val();
var tc;
if(t=="1")
{
tc="第一页";
}
else if(t=="2")
{
tc="第二页";
}
else if(t=="3")
{
tc="第三页";
}
var src="forBhbwh?dto.tc="+tc;
$("#frm").attr("src",src);
}
</script>
</head>
<body bgcolor="#FFFFFF">
<select id="xz">
<option id="1" value="1">1</option>
<option id="2" value="2">2</option>
<option id="3" value="3">3</option>
</select>
<br/>
<iframe id="frm" src="forBhbwh?dto.tc=第一页" width="820" height="352" frameborder="0" scrolling="auto"></iframe>
</body>
</html>
但是这样做明显有缺点:如果以后需要改iframe的地址就要改两次,一次是submit函数,一次是iframe的src地址,明显这样做很不好。而且,万一之后要求默认选择的不是1而是2或3,也得改iframe地址,否则会出现如下可笑的情况:
为了防止这些情况,最后还得在document.ready函数里执行一次submit函数。这样即使初始地址给的是错的,也会在执行submit函数的过程中恢复过来。但是就怕什么时候页面加载完毕但还没执行submit函数时电脑卡死了,那还是会让顾客看到上面那种矛盾的情况。但也只有这样了。给出最后“接近完美”解决该乱码问题的代码:
<!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=utf-8">
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title></title>
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/public/jqGrid/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
submit();
$('#xz').change(submit);
});
function submit()
{
var t=$("#xz").val();
var tc;
if(t=="1")
{
tc="第一页";
}
else if(t=="2")
{
tc="第二页";
}
else if(t=="3")
{
tc="第三页";
}
var src="forBhbwh?dto.tc="+tc;
$("#frm").attr("src",src);
}
</script>
</head>
<body bgcolor="#FFFFFF">
<select id="xz">
<option id="1" value="1">1</option>
<option id="2" value="2">2</option>
<option id="3" value="3">3</option>
</select>
<br/>
<iframe id="frm" src="forBhbwh?dto.tc=第一页" width="820" height="352" frameborder="0" scrolling="auto"></iframe>
</body>
</html>