简体   繁体   English

javascript - 从iframe中的元素获取数据

[英]javascript - get data from an element within an iframe

I've got this <iFrame> [name="data"] with a page displaying a <span> , with id="weergave_percentage". 我有一个<iFrame> [name =“data”],页面显示<span> ,id =“weergave_percentage”。 I want to get the numbers from that span into a javascript function. 我希望将该范围内的数字转换为javascript函数。 I currently made this, but it returns: null 我目前做了这个,但它返回:null

var percentage = window.frames['data'].document.getElementById('weergave_percentage');
alert(percentage);

What am I doing wrong and how can I do what I want? 我做错了什么,我怎么能做我想做的事?

Note: the span containing the data, only contains numbers (float), and no other HTML formatting of any kind. 注意:包含数据的范围仅包含数字(浮点数),并且不包含任何其他类型的HTML格式。 Also, I've put the piece of javascript after the code for the iFrame, to make sure the iFrame is loaded when I try to get the data from the iFrame. 另外,我在iFrame的代码之后添加了一段javascript,以确保在我尝试从iFrame获取数据时加载了iFrame。 Also, the iFrame's src is on the same domain as the main page. 此外,iFrame的src与主页面位于同一个域中。

If iframe src links to other domain than the one your pages is displayed from - then you can't get that data (browsers prevernt cross-site scripting). 如果iframe src链接到显示您的页面之外的其他域 - 那么您将无法获取该数据(浏览器会提供跨站点脚本)。

Other than that - this could should work: 除此之外 - 这可能会起作用:

$('iframe').contents().find('#weergave_percentage').html();

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

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