合并onmouseover事件和<a> tag</a>

时间:2015-03-18 11:08:31

标签: javascript html

这是包含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>

但是,当您将鼠标移到图像上时,它会显示

  

未捕获的TypeError:无法读取未定义的属性'src'

代码有什么问题? 在此先感谢!!

2 个答案:

答案 0 :(得分:0)

document.image未定义。我想你需要document.images。虽然你的代码真的很乱。 :)

&#13;
&#13;
<!--
if (document.images){
	var image1 = new Image();
	image1.src = "Dark Swampert.jpg";
	var image2 = new Image();
	image2.src = "Tulips.jpg";}
	
function printPage(){
	window.print();}
	
//-->
&#13;
body{background-color: aquamarine;}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

image1和image2必须在全球范围内。