简体   繁体   English

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

[英]How to include image in frame from another html file?

i am currently having problem including image in frame from another file, what i am doing is creating a html page with two frame having code : 我目前有问题,包括来自另一个文件的框架图像,我正在做的是创建一个具有两个框架的html页面,其代码如下:

<html>

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

</frameset>

</html>

Then, in aa.html there are three links and on clicking the first link an image should open in frame with id="2". 然后,在aa.html中有三个链接,并且在单击第一个链接时,图像应以id =“ 2”的框架打开。

aa.html

<html>

<head>
<link rel="import" href="cook.html">
<script>
function fun1()
{
    document.getElementById('2').src="b.html";
}
</script>
</head>
<body>
<a href="#" onclick="fun1()">Home1</a>
<a href="fdsf">Hom2</a>
<a href="fdsf">Home3</a>
</body>

</html>

then b.html shows image in the frame,like this : 然后b.html在框架中显示图像,如下所示:

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

But this does not work.thanks in advance. 但这是行不通的。

You can't do what you want in this way. 您无法以这种方式做自己想做的事。

This function: 该功能:

    function fun1() {
        document.getElementById('2').src="b.html";
    } 

is working IN IFRAME, and not in MAIN WINDOW. 在IFRAME中工作,而不在MAIN WINDOW中工作。 Try look at window.postMessage methods. 尝试看看window.postMessage方法。


UPDATE working with window.postMessage look like that: 使用window.postMessage进行的UPDATE如下所示:

1) in child window will be function 1)在子窗口中将起作用

  function fun1() {
   window.postMessage(JSON.stringify({"id":2,"src":"b.html"}),'http://parent.window.com');
  } 

2) in parent window, you should listen for this event, so add on start of page: 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);}

}

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

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