结合onmouseover事件和<a>标签

Incorporating onmouseover event and <a> tag

本文关键字:gt 标签 onmouseover lt 结合 事件      更新时间:2023-09-26

这是包含onmouseover事件和标签的代码:

您可以将image1.src和image2.src更改为您喜欢的任何图像

<!--
if (document.image){
	var image1 = new Image();
	image1.src = "Dark Swampert.jpg";
	var image2 = new Image();
	image2.src = "Tulips.jpg";}
	
function printPage(){
	window.print();}
	
//-->
body{background-color: aquamarine;}
<head><title> Cheater </title></head>
<body>
<p> Move the mouse over the image to see results: </p>
<a href="#" onmouseover="document.myImage.src=image2.src;" onmouseout="document.myImage.src=image1.src;">
<img name="myImage" src="Dark Swampert.jpg" width=120 height=160/>
</a>
<br><input type=button value="Print this Page!" onClick="printPage();"/>
</body>

然而,当你把鼠标移到图像上时,它会显示

未捕获的类型错误:无法读取未定义的属性"src"

代码出了什么问题?提前感谢!!

document.image未定义。我想你需要document.images。尽管你的代码真的一团糟。:)

<!--
if (document.images){
	var image1 = new Image();
	image1.src = "Dark Swampert.jpg";
	var image2 = new Image();
	image2.src = "Tulips.jpg";}
	
function printPage(){
	window.print();}
	
//-->
body{background-color: aquamarine;}
<head><title> Cheater </title></head>
<body>
<p> Move the mouse over the image to see results: </p>
<a href="#" onmouseover="document.myImage.src=image2.src;" onmouseout="document.myImage.src=image1.src;">
<img name="myImage" src="Dark Swampert.jpg" width=120 height=160/>
</a>
<br><input type=button value="Print this Page!" onClick="printPage();"/>
</body>

var image1 = new Image();
image1.src = "Dark Swampert.jpg";
var image2 = new Image();
image2.src = "Tulips.jpg";

图像1和图像2必须在全局范围内。