对象只能添加到创建它的页面上,不可以跨页面

本文介绍了一个使用HTML与JavaScript实现的多页面导航系统。通过主页面main.html中的iframe加载iframe.html,后者负责向主页面添加导航条内容,并跳转至内容页面content.html。文章重点讨论了DOM对象在不同页面间的交互问题。

 假如有3个页面

 main.html(主要负责iframe中间页面) 包含 iframe.html(主要负责出数据,跳转到真实内容页面)

 内容页面为 content.html(真实内容)

  这几个页面加在一起,主要实现导航功能.

  中间注意DOM 对象的跨页面问题!!!!!

main.html

<html>
    
<head>
        
<title>Main</title>
    
</head>
    
<body>
        
<div id="navigator"><label>Navigator Bar : </label><href="main.html">Home</a><label> &gt; </label></div>
        
<iframe src="iframe.html" name="content" width="90%" height="90%"></iframe>
    
</body>
</html>
iframe.html
<html>
    
<head>
        
<title>iframe</title>
        
<script type="text/javascript">
            
function navigator(anchor){
                
//navigator div element in parent page
                var navigatorBar = window.parent.document.getElementById("navigator");
                
                
//attention!!!
                //when create an object,you can only use it in the page where create the object,cann't cross pages
                var newAnchor = window.parent.document.createElement("a");
                newAnchor.href 
= anchor.href;
                newAnchor.target 
= "content";
                newAnchor.appendChild(window.parent.document.createTextNode(anchor.firstChild.nodeValue));

                
//append anchor to parent page
                navigatorBar.appendChild(newAnchor);
            }

        
</script>
    
</head>
    
<body>
        
<href="content.html"onclick="navigator(this)">Add this anchor to parent page</a>
    
</body>
</html>
content.html
<html>
    
<head>
        
<title>Content</title>
    
</head>
    
<body>
        
<p>Content Page</p>
    
</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值