我有index.htm和index2.htm两个页面。index上有一个按钮remove text,可以删除页面上的元素内容然后跳转到index2,然后点击浏览器的后退按钮,index上被删除的元素又重新出现,我的问题是如何删除一个元素,可以让浏览器后退的时候也看不到这个被删元素?谢谢!以下是index.htm代码:<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7/jquery.min.js"></script>
<title>test</title>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$('#content').html("<p>aaaaaabbbaaaaaa</p>");
});function hideText()
{
$('#content').remove();
window.location.href="index2.htm";
}</script>
</head>
<body>
<div id="content">
</div>
<div>
<input id="hide" type="button" value="remove text" onclick="hideText();"/>
</div>
</body>
</html>
以下是index2.htm代码<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<div id="content">
page2
</div>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7/jquery.min.js"></script>
<title>test</title>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$('#content').html("<p>aaaaaabbbaaaaaa</p>");
});function hideText()
{
$('#content').remove();
window.location.href="index2.htm";
}</script>
</head>
<body>
<div id="content">
</div>
<div>
<input id="hide" type="button" value="remove text" onclick="hideText();"/>
</div>
</body>
</html>
以下是index2.htm代码<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<div id="content">
page2
</div>
</body>
</html>
即点击按钮后,保存一个cookie值,然后在index2.htm的head部分验证这个cookie是否存在,如果存在,
$('#content').remove();
不同浏览器对于后退的实现不太一样,IE是会执行onload,而ff,safari等浏览器不会执行,而是保持原样。
“然后在index2.htm的head部分验证这个cookie是否存在”,应该是index1.thm的head部分验证这个cookie是否存在吧?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function test(){
var a=document.getElementById("test");
if(a){
var p=a.parentNode;
p.removeChild(a);
document.cookie="removes=true;";
}
window.location.href="http://www.baidu.com";
}
function init(){
var a=document.cookie;
var b=a.indexOf("removes=");
var c=a.indexOf(";",b);
if(c==-1){
c=a.length;
}
var d=a.substring(b+8,c);
if(d=="true"){
var a=document.getElementById("test");
var p=a.parentNode;
p.removeChild(a);
}
}
window.onload=init;
</script>
</head><body>
<div id="test" style="width:400px; height:400px; background-color:#0F0"></div>
<input type="button" value="test" onclick="test()">
</body>
</html>
这样试试