javascript - get data from an element within an iframe

≯℡__Kan透↙ 提交于 2019-12-23 17:18:15

问题


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?

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.


回答1:


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();


来源:https://stackoverflow.com/questions/7944937/javascript-get-data-from-an-element-within-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!