简体   繁体   English

从iframe访问某些值并在父页面Javascript中使用它

[英]Accessing certain value from iframe and using it in parent page Javascript

Inside my iframe i have a dial that spits out angle when the needle moves. 在我的iframe中,我有一个拨盘,当针头移动时该拨盘会散开角度。 Instead of getting both dial and needle both inside frame, is it possible to show the angle value somewhere else in my parent page outside the iframe. 可以在iframe之外的父页面中的其他位置显示角度值,而不是在表盘中同时显示刻度盘和指针。 This is the code i have. 这是我的代码。

function showIframeContent(id) {
  var iframe = document.getElementById(id);
    try {
      var doc = (iframe.contentDocument)? window.frames[clkwise1].document.getElementId("demo").value;
      alert(doc.body.innerHTML);
    }
    catch(e) {
       alert(e.message);
    }
  return false;
}

The standard and cross-browser way to access the content of an iframe is: 访问iframe内容的标准和跨浏览器方式是:

iframe.contentWindow.document

I would expect this to work in your case: 我希望这在您的情况下有效:

document.getElementById("clkwise1").contentWindow.document.getElementById("demo").value

(I'd need to see the page itself to confirm it - for example I assume "demo" is an input element. Also, note your typo in getElementById) (我需要查看页面本身以进行确认-例如,我假设“ demo”是输入元素。此外,请注意您在getElementById中的错字)

Live demo: http://jsfiddle.net/ZWC6v/ 现场演示: http//jsfiddle.net/ZWC6v/

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

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