javascript - 从iframe中的元素获取数据 [英] javascript - get data from an element within an iframe

查看:120
本文介绍了javascript - 从iframe中的元素获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有< iFrame> [name =data],页面显示< span> ,id =weergave_percentage。
我想将该范围内的数字转换为javascript函数。
我目前做了这个,但它返回:null

I've got this <iFrame> [name="data"] with a page displaying a <span>, with id="weergave_percentage". I want to get the numbers from that span into a javascript function. I currently made this, but it returns: 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?

注意:包含数据的范围仅包含数字(浮点数),并且没有任何其他类型的HTML格式。
另外,我已经在iFrame的代码之后添加了一段javascript,以确保在我尝试从iFrame获取数据时加载了iFrame。
此外,iFrame的src与主页在同一个域中。

Note: the span containing the data, only contains numbers (float), and no other HTML formatting of any kind. 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. 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).

除此之外 - 这可能会起作用:

Other than that - this could should work:

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

这篇关于javascript - 从iframe中的元素获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆