关于Javascript图片库改进的问题

最近在学习Javascript编程的艺术,其中有一章节说的是图片库的问题,实现点击链接,在网页不跳转的情况下,更换下方的图片的效果。

现在有一个问题,当我点击按钮,网页会跳转,我也在匿名函数里写了点击返回false的,为什么还会跳转呢?求助,求助,谢谢!

以下是代码:

https://img3.mukewang.com/5c7cb1ea00017fcc06630857.jpg

叮当猫咪
浏览 626回答 2
2回答

茅侃侃

执行prepareGallery方法:prepareGallery();

倚天杖

实质是你的prepareGallery函数没有执行过,所以没有起效果。你在下面执行,或者声明后马上执行就可以:(function prepareGallery() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!document.getElementsByTagName) {return false;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!document.getElementById) {return false;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!document.getElementById("imageGallery")) {return false;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var gallery = document.getElementById("imageGallery");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var links = gallery.getElementsByTagName("a");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (var i=0; i<links.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;links[i].onclick = function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showPic(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false; //屏蔽游览器跳转的行为&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})();&nbsp; //这里立即执行了函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript