您还可以将“this”参数传递给 closeFrame 函数调用,该函数将为您提供图像元素。然后,您可以display: none
在该元素上进行设置:
<html>
<head>
<script language="JavaScript" type="text/javascript">
function makeFrame(image_element) { <!-- Take the image_element as a parameter -->
image_element.style.display = "none"; <!-- Set image element to not display -->
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src","http://www.google.com");
ifrm.style.display = "block";
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.appendChild(ifrm);
}
function closeFrame(){
ifrm.style.display = "none";
}
</script>
</head>
<body>
<!-- Pass this into makeFrame where this is the image element -->
<img src="images/largepic.jpg" onmouseover=makeFrame(this) onmouseout=closeFrame() height="100px" width="100px" />
</body>
</html>
正如 Shmiddty 所说,closeframe()
一旦你设置display: none
为 ,就会被调用。您可能想在 iframe 本身而不是 img 元素上设置 onmouseout 事件。
根据 OP 的评论更新我的答案:
这是将鼠标移出时关闭 IFrame 所需的代码。这也恢复了原始图像。
<html>
<head>
<script language="JavaScript" type="text/javascript">
function makeFrame(image_element) { <!-- Take the image_element as a parameter. -->
image_element.style.display = "none"; <!-- Set image element to not display. -->
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src","http://www.google.com");
ifrm.style.display = "block";
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
ifrm.onmouseout=closeFrame; <!-- Close the IFrame when the mouse moves out of it. -->
document.body.appendChild(ifrm);
}
function closeFrame(){
ifrm.style.display = "none";
var image_element = document.getElementById("myImg"); <!-- Show the image that was hidden when showing the iframe. -->
image_element.style.display = "inline";
}
</script>
</head>
<body>
<!-- Pass this into makeFrame where this is the image element -->
<img id="myImg" src="images/largepic.jpg" onmouseover=makeFrame(this) height="100px" width="100px" />
</body>
</html>
这是通过将onmouseout
事件绑定到 IFrame 来实现的。closeFrame
这告诉浏览器在将鼠标移出 IFrame 时调用该函数。然后在closeFrame
函数中恢复之前隐藏的图像。