我的 Javascript 图像预加载器代码有问题
我似乎无法理解为什么我的图像仍然不会与我的代码一起显示。我的所有文件夹和图像都正确命名并相应放置。但是,如果有人可以查看我的代码并查看是否存在错误并让该代码正常运行。那就太好了。
<!DOCTYPE html>
<html>
<head>
<title>Sample page</title>
<script type="text/javascript">
function changeImage(replacement)
{
document.getElementById("main_image").src = replacement;
return false;
}
// End -->
</script>
</head>
<body>
<p>
<a href="javascript:changeImage('image1.jpg')">Image 1</a>
<a href="javascript:changeImage('image2.jpg')">Image 2</a>
<a href="javascript:changeImage('image3.jpg')">Image 3</a>
<a href="javascript:changeImage('image4.jpg')">Image 4</a>
</p>
<p>
<img id="main_image" src="image-viewer/blank.jpg" alt="" /></p>
<p><center>
<font face="arial, helvetica" size"-2">
</center><p>
</font></body>
</html>
I can't seem to understand why my images still won't show up with my code. All my folders and images are correctly names and placed accordingly. However, if someone could look over my code and see if there is an error there and get this code to run properly. It would be great.
<!DOCTYPE html>
<html>
<head>
<title>Sample page</title>
<script type="text/javascript">
function changeImage(replacement)
{
document.getElementById("main_image").src = replacement;
return false;
}
// End -->
</script>
</head>
<body>
<p>
<a href="javascript:changeImage('image1.jpg')">Image 1</a>
<a href="javascript:changeImage('image2.jpg')">Image 2</a>
<a href="javascript:changeImage('image3.jpg')">Image 3</a>
<a href="javascript:changeImage('image4.jpg')">Image 4</a>
</p>
<p>
<img id="main_image" src="image-viewer/blank.jpg" alt="" /></p>
<p><center>
<font face="arial, helvetica" size"-2">
</center><p>
</font></body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除
return false;
行。(这只能用于
onclick
处理程序)当您从
javascript:
URI 返回任何值时,浏览器将导航到返回值。当您从事件处理程序返回
false
时,该事件将被抑制。Remove the
return false;
line.(This should only be used for
onclick
handlers)When you return any value from a
javascript:
URI, the browser navigates to the return value.When you return
false
from an event handler, the event is suppressed.如果没有示例页面,很难说清楚。如果您的所有图像都位于“image-viewer”目录下,那么您需要将其添加到所有链接的路径中,即
It's hard to tell without an example page. If all your images are under the 'image-viewer' directory then you will need to add that to the path in all your links i.e
image1.jpg、image2.jpg、image3.jpg 和 image4.jpg 是否与您粘贴的文件位于同一文件夹中?如果没有,您需要在
javascript:changeImage()
调用中指定该路径。Is image1.jpg, image2.jpg, image3.jpg, and image4.jpg in the same folder as the file you pasted? If not, you need to specify the path to that in your
javascript:changeImage()
call.