Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在另一个html文件的框架中包含图像?_Javascript_Html - Fatal编程技术网

Javascript 如何在另一个html文件的框架中包含图像?

Javascript 如何在另一个html文件的框架中包含图像?,javascript,html,Javascript,Html,我目前在从另一个文件的框架中包含图像时遇到问题,我正在做的是创建一个html页面,其中包含两个框架代码: <html> <frameset cols="20%,80%"> <frame src="aa.html" id="1"> <frame src="" id="2"> </frameset> </html> 然后,在aa.html中有三个链接,单击第一个链接时,图像将在帧中打开,id=“2” aa.html

我目前在从另一个文件的框架中包含图像时遇到问题,我正在做的是创建一个html页面,其中包含两个框架代码:

<html>

<frameset cols="20%,80%">
<frame src="aa.html" id="1">
<frame src="" id="2">

</frameset>

</html>

然后,在aa.html中有三个链接,单击第一个链接时,图像将在帧中打开,id=“2”

aa.html
函数fun1()
{
document.getElementById('2').src=“b.html”;
}
然后b.html在框架中显示图像,如下所示:

<html>
<head>
</head>
<body>
<img src="ima.png"/>
</body>
</html>


但是这不起作用。提前谢谢。

用这种方式你不能做你想做的事

此功能:

    function fun1() {
        document.getElementById('2').src="b.html";
    } 
正在IFRAME中工作,而不是在主窗口中。 尝试查看window.postMessage方法


更新使用window.postMessage如下所示:

1) 在子窗口中,将显示函数

  function fun1() {
   window.postMessage(JSON.stringify({"id":2,"src":"b.html"}),'http://parent.window.com');
  } 
2) 在父窗口中,您应该侦听此事件,所以请在页面开头添加:

window.addEventListener("message", receiveMessage, false);

function receiveMessage( event )
{
  if (event.origin !== "http://example.org:8080")
    return;
  try{
      var data = JSON.parse(event.data);
      if( data.id != undefined && data.src != undefined ){
         document.getElementById(data.id).src = data.src;
      }
  } catch( exception ){ console.log( exception);}

}

你能给出一个例子,以及在哪里包含这个脚本吗
window.addEventListener("message", receiveMessage, false);

function receiveMessage( event )
{
  if (event.origin !== "http://example.org:8080")
    return;
  try{
      var data = JSON.parse(event.data);
      if( data.id != undefined && data.src != undefined ){
         document.getElementById(data.id).src = data.src;
      }
  } catch( exception ){ console.log( exception);}

}