Javascript 单击即可在两个网页之间更改两个iFrame的相同内容

Javascript 单击即可在两个网页之间更改两个iFrame的相同内容,javascript,html,iframe,click,Javascript,Html,Iframe,Click,我需要更改两个网页之间的两个iFrame的相同内容,只需单击一下。就像powerpoint,您只需单击一下,就可以在两个屏幕上切换到新幻灯片 我希望你能给我答案! 谢谢 如果您熟悉更改一个iframe的内容,那么在多个iframe上进行更改是相当简单的。如果您确信永远不需要更改两个以上的iFrame,那么最简单的解决方案是使用Javascript执行以下操作: // function that updates a single iframe function updateIframe(ifram

我需要更改两个网页之间的两个iFrame的相同内容,只需单击一下。就像powerpoint,您只需单击一下,就可以在两个屏幕上切换到新幻灯片

我希望你能给我答案!
谢谢

如果您熟悉更改一个iframe的内容,那么在多个iframe上进行更改是相当简单的。如果您确信永远不需要更改两个以上的iFrame,那么最简单的解决方案是使用Javascript执行以下操作:

// function that updates a single iframe
function updateIframe(iframe) {
  var iframeDocument = iframe.contentWindow.document;
  // iframeDocument is the iframe's document, so commands
  // like the ones below will work.
  // iframeDocument.appendChild();
  // iframeDocument.body.innerHTML = "<h1>Hello World</h1>";
}

// get the iframes from the DOM by their html id attribute
var iframe1 = document.getElementById('iframe1');
var iframe2 = document.getElementById('iframe2');

// bind a click event listener to the document body that
// will run updateIframe on both iframes
document.body.addEventListener('click', function() {
  updateIframe(iframe1);
  updateIframe(iframe2);
});

这个解决方案在到网页之间有效吗?当我读到你的问题时,我认为你的意思是你在一个网页的iFrame中加载了两个不同的网页。如果您正在寻找网页上下文之外的内容来更改网页,则需要某种操作系统宏或应用程序扩展。