我正在尝试在 html onmouseover 属性中运行 javascript 函数来交换图片。图片全部显示,但是onmouseover不起作用;我想我以错误的方式逃脱(浏览器显示>)-但是我找不到问题所在非常感谢您的帮助!
一些代码:
...
<?php
echo "<img src=\"$main_img_small\" alt=\"\" onmouseover=\"<script language=\"JavaScript\" type=\"text/javascript\" src=\"mouseover.js\"></script><script type=\"text/javascript\">swapImage(0);</script>\">";
echo "<img src=\"$var1_small\" alt=\"\" onmouseover=\"<script language=\"JavaScript\" type=\"text/javascript\" src=\"mouseover.js\"></script><script type=\"text/javascript\">swapImage(1);</script>\">";
echo "<img src=\"$var2_small\" alt=\"\" onmouseover=\"<script language=\"JavaScript\" type=\"text/javascript\" src=\"mouseover.js\"></script><script type=\"text/javascript\">swapImage(2);</script>\">";
?>
...
mouseover.js(存储在同一文件夹中):
var phpvar1_large = <?php echo json_encode($var1_large); ?>;
var phpvar2_large = <?php echo json_encode($var2_large); ?>;
var phpvar3_large = <?php echo json_encode($var3_large); ?>;
var phpvar4_large = <?php echo json_encode($var4_large); ?>;
if(!phpvar1_large){
var imgArray = new Array(
'<?=$main_img; ?>'
);
}else if(!phpvar2_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>'
);
}else if(!phpvar3_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>',
'<?=$var2_large; ?>',
'<?=$var3_large; ?>'
);
}else if(!phpvar4_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>',
'<?=$var2_large; ?>',
'<?=$var3_large; ?>'
);
}else if(phpvar4_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>',
'<?=$var2_large; ?>',
'<?=$var3_large; ?>',
'<?=$var4_large; ?>'
);
}
function swapImage(imgID) {
var theImage = document.getElementById('theImage');
var newImg;
newImg = imgArray[imgID];
theImage.src = newImg;
}
function preloadImages() {
for(var i = 0; i < imgArray.length; i++) {
var tmpImg = new Image;
tmpImg.src = imgArray[i];
}
}
浏览器输出:
...
<div id="image">
<img id="theImage" src="http://ecx.images-amazon.com/images/I/41ioC67AdgL.jpg" alt="" />
</div>
<div id="thumbs">
<img src="http://ecx.images-amazon.com/images/I/41ioC67AdgL._SL75_.jpg" alt="" onmouseover="<script language="JavaScript" type="text/javascript" src="mouseover.js"></script><script type="text/javascript">swapImage(0);</script>"><img src="http://ecx.images-amazon.com/images/I/41Yrx4v10TL._SL75_.jpg" alt="" onmouseover="<script language="JavaScript" type="text/javascript" src="mouseover.js"></script><script type="text/javascript">swapImage(1);</script>"><img src="http://ecx.images-amazon.com/images/I/41bzT-hN9mL._SL75_.jpg" alt="" onmouseover="<script language="JavaScript" type="text/javascript" src="mouseover.js"></script><script type="text/javascript">swapImage(2);</script>">
<br />
</div>
...
所以,在你看到的小图右侧 ">
注意:如果您想知道,我最终会尝试在 wordpress 页面上运行它,这就是 javascript 命令看起来如此的原因(见此处:http ://codex.wordpress.org/Using_Javascript )