2010-04-11 41 views
1

我的网站有一个iframe,它是用html内容动态填充的。该HTML通常包含命名锚,在IE/Chrome中可以正常工作,但在Firefox中,它会重新打开iframe中的整个页面。Firefox点击命名锚时在iframe中重新加载父页面

下面是一个例子:在firefox中加载页面,滚动到iframe的底部,点击“返回顶部”链接,你会看到我在说什么。

<html><head></head><body onload="setFrameContent();"><script> 

var htmlBody = '<html> <head></head> <body>' + 
'<a name="top"><h1>top</h1></a>' + 
'<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>' + 
'<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>' + 
'<a href="#top">back to top</a></body> </html> '; 

function setFrameContent(){ 
    if (frames.length > 0) { 
     var d = frames[0].document; 
     d.open(); 
     d.write(htmlBody); 
     d.close(); 
    } 
} 

</script> 
<h1>Here's an iframe:</h1> 
<iframe id="htmlIframe" style="height: 400px; width: 100%"><p>Your browser does not support iframes.</p></iframe> 
</body></html> 

有没有想法?

回答

0

我有同样的问题,并发现这种解决方法。

<html> 
<head> 
<script> 
var htmlBody = '<html> <head></head> <body>' + 
'<a id="top"><h1>top</h1></a>' + 
'<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>' + 
'<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>' + 
'<a href="javascript:parent.gotoAnchor(\'top\')">back to top</a></body> </html> '; 

function setFrameContent(){ 
    if (frames.length > 0) { 
     var d = frames[0].document; 
     d.open(); 
     d.write(htmlBody); 
     d.close(); 
    } 
} 

function gotoAnchor(id) 
{ 
    var el = null; 
    el = window.frames['myiframe'].document.getElementById(id); 
    window.frames['myiframe'].scrollTo(0,el.offsetTop); 
} 
</script> 
</head> 
<body onload="setFrameContent();"> 
<h1>Here's an iframe:</h1> 
<iframe name="myiframe" id="htmlIframe" style="height: 400px; width: 100%"><p>Your browser does not support iframes.</p></iframe> 
</body> 
</html> 

希望这会有所帮助。

+0

不错!我已经对页面进行了重新设计,因此我无法使用您的解决方案,但希望这可以帮助其他人。 – masty 2010-06-17 21:45:35

相关问题