简体   繁体   English

div innerhtml对象访问父HTML div

[英]div innerhtml object access parent html div

I have a main.html containing the following div: 我有一个main.html包含以下div:

<div id="main">

I want to dynamically load html in this div (say page1.html and page2.html ). 我想在此div中动态加载html(例如page1.htmlpage2.html )。 I am able to accomplish this through the following code : 我可以通过以下代码完成此操作:

document.getElementById("main").innerHTML='<object type="text/html" data="page1.html" ></object>';

Now, I want to have a button id="btn1" in my page1.html , which should load page2.html into div "main" of my parent main.html . 现在,我想在我的page1.html的按钮ID =“BTN1”,这应该page2.html加载到DIV我父母main.html中的“主力”。 However, in page1.html, I am not able to locate my container div "main" 但是,在page1.html中,我无法找到我的容器div“ main”

document.getElementById("main") // returns null

Thanks in advance for the help! 先谢谢您的帮助!

Your issue is trying to use a single page's DOM to interact with content loaded in through an iframe or object . 您的问题是尝试使用单个页面的DOM与通过iframeobject加载的内容进行交互。 Using these makes life very difficult for a JavaScript developer. 使用这些使JavaScript开发人员的生活非常困难。

If you are using only plain JavaScript, I would suggest you instead use an AJAX request to load in HTML directly through the DOM instead of transcluding this content using old methods. 如果仅使用纯JavaScript,建议您使用AJAX请求直接通过DOM加载HTML,而不是使用旧方法来替换此内容。

Here's a beginner's guide to plain-JS AJAX. 这是纯JS AJAX的初学者指南。 Frameworks like jQuery simplify the process immensely. jQuery之类的框架极大地简化了该过程。
http://code.tutsplus.com/articles/how-to-make-ajax-requests-with-raw-javascript--net-4855 http://code.tutsplus.com/articles/how-to-make-ajax-requests-with-raw-javascript--net-4855

Otherwise, no. 否则,不会。 You cannot access that node's content because of Cross-Site Scripting security features. 由于跨站点脚本安全功能,您无法访问该节点的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM