JavaScript中onclick函数return false 未被触发是怎么回事?

按照javascript dom 编程艺术关于网页显示图片的那部分内容敲出来的代码,其中有一段需要输入onclick="showPic(this);return false;"用于阻止网页弹出新的链接来查看图片,但是最后运行的时候还是弹出新的链接了,具体代码如下:


<!DOCTYPE html> <html lang="en"> <head>   <meta charset="utf-8"/>   <title>My Gallery</title> </head> <body>   <h1>snapshots</h1>   <ul>     <li>     <a href="images/do or die.jpg" onclick="showPic(this);return false;" title="do or die">DO</a>     </li>     <li>     <a href="images/hand.jpg" onclick="showPic(this);return false;" title="hand">HAND</a>     </li>     <li>     <a href="images/phrase.jpg" onclick="showPic(this);return false;" title="phrase">Phrase</a>     </li>   </ul>   <img id="placeholder" src="images/dog.jpg" alt="My Gallery"/>   <script>   function showPic(whichpic)   {var source=whichpic.getAttribute ("href");   var placeholder=document.getElenmentById ("placeholder");   placeholder.setAttribute("src",source);}   </script> </body> </html>


猛跑小猪
浏览 929回答 2
2回答

慕标琳琳

首先是你的id获取单词都拼错了,其次是a标签href指向问题和点击事件冲突<!DOCTYPE html><html><head><meta charset="utf-8"/><title>My Gallery</title></head><body ><h1>snapshots</h1><ul><li>&nbsp;<a&nbsp;href="javascript:;"&nbsp;date-href="zhc1.jpg"&nbsp;onclick="showPic(this);return&nbsp;false;"&nbsp;title="do&nbsp;or&nbsp;die">DO</a>&nbsp;</li>&nbsp;<li>&nbsp;<a&nbsp;href="javascript:;"&nbsp;date-href="zhc2.jpg"&nbsp;onclick="showPic(this);return&nbsp;false;"&nbsp;title="hand">HAND</a>&nbsp;</li>&nbsp;<li>&nbsp;<a&nbsp;href="javascript:;"&nbsp;date-href="zhc3.jpg"&nbsp;onclick="showPic(this);return&nbsp;false;"&nbsp;title="phrase">Phrase</a>&nbsp;</li></ul><img id="placeholder" src="zixun_video_pic.jpg" alt="My Gallery"/><script>&nbsp;&nbsp;function&nbsp;showPic(whichpic){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;source=whichpic.getAttribute&nbsp;("date-href");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;placeholder=document.getElementById&nbsp;("placeholder");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;placeholder.setAttribute("src",source);&nbsp;}</script></body></html>

慕工程0101907

获取id是document.getElementById('')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript