简体   繁体   English

为什么$(window ['iframeName']。document.body).html()在我更改.attr('src')时不起作用

[英]why $(window['iframeName'].document.body).html() doesn't work when i change .attr('src')

Why doesn't 为什么不

$(window['iframeName'].document.body).html()

...work when i change .attr('src') ...当我更改.attr('src')

When i set the src attribute of iframe to any url when i creating the page, this code 当我在创建页面时将iframe的src属性设置为任何网址时,此代码

$(window['iframeName'].document.body).html() 

...will work. ...将工作。

But when i change the src attribute by .attr('src',"www.google.com.sa") and wait to load the page, 但是,当我通过.attr('src',"www.google.com.sa")更改src属性并等待加载页面时,

$(window['iframeName'].document.body).html() 

...will doesn't work. ...将不起作用。

Where is the problem ? 问题出在哪儿 ?

尝试:

.attr('src',"http://www.google.com.sa")

A couple of things: 有两件事:

Firstly, setting attr('src') will create a new history item. 首先,设置attr('src')将创建一个新的历史记录项。 You should consider if you actually want to do that, eg if someone can click the back button and get the last page. 您应该考虑是否确实要这样做,例如有人可以单击“后退”按钮并获得最后一页。 If you don't want the changed state in the history (desirable for ads or apps that use iframes instead of outright ajaex), then you would want to change the document's location instead. 如果您不希望更改历史记录的状态(对于使用iframe而不是完全ajaex的广告或应用程序而言是理想的),则可以改用文档的位置。

Secondly, I find it much easier to interact with the page like this: 其次,我发现与这样的页面进行交互要容易得多:

$("#frameID").contents().find("jquery selector");

You can get the full HTML using a call ilke this: 您可以使用以下调用获得完整的HTML:

$("#frameID").contents(document).children().html()

When you load a page from a different domain in the iframe, you can't access it. 从iframe中的其他域加载页面时,您将无法访问它。 This is for security reasons. 这是出于安全原因。

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

相关问题 为什么我不能删除document.body属性? - Why can't I delete the document.body property? 我何时应该在窗口与文档与document.body上观察Javascript事件? - When should I observe Javascript events on window vs. document vs. document.body? 附加 document.body 的 innerHTML 后,Jquery 的日期选择器不起作用 - Jquery's datepicker doesn't work after appending document.body's innerHTML 在document.body中更改图像 - Change image in document.body 为什么我需要使用Mootools Element方法扩展document.body $(document.body)? - Why do I need to do $(document.body) to extend document.body with Mootools Element methods? 我无法将 PreactX 组件直接渲染到 document.body 中 - I can't render PreactX component directly into document.body 为什么我的javascript中的document.body为null? - Why is document.body null in my javascript? 当在文档末尾添加视口高度和窗口scrollTop位置时,jQuery $(document.body).height()不相同 - Jquery $(document.body).height() is not same when viewport height is added with the window scrollTop position, at the end of document 当document.body存在时执行js - Execute js when document.body exists Window.scroll / scrollTo方法在position:fixed div而不是document.body中工作 - Window.scroll / scrollTo method to work in position:fixed div rather then document.body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM