鍍金池/ 問答/網(wǎng)絡安全  HTML/ 《JS DOM編程藝術 第2版》第6章問題, return !showpic(t

《JS DOM編程藝術 第2版》第6章問題, return !showpic(this); 為什么會無效?

《Javascript DOM》(第2版)第6章的一個demo
JS代碼:

<script>
            function prepareGallery()
            {
                if(!document.getElementById("gallery"))return false;
                if(!document.getElementsByTagName)return false;
                var gallery=document.getElementById("gallery");
                var links=gallery.getElementsByTagName("a");
                for(i=0;i<links.length;i++)
                {
                    links[i].onclick=function()
                    {
                        return !showpic(this);
                    }
                }
            }
            
            function showpic(x)
            {
                var changepic=document.getElementById("placeholder");
                var source=x.getAttribute("href");
                changepic.setAttribute("src",source);
                var description=document.getElementById("description");
                var text=x.firstChild.nodeValue;
                description.firstChild.nodeValue=text;
            }
            
            function addLoadEvent(func){
                var oldonload=window.onload;
                if (typeof window.onload!='function')
                {
                    window.onload=func;
                }
                else{
                    window.onload=function()
                    {
                        oldonload();
                        func();
                    }
                }
            }
            addLoadEvent(prepareGallery);
        </script>

HTML代碼:

<h1>標題</h1>
        <ul id="gallery">
            <li>
                <a href="img/擺渡.jpg" title="擺渡人" >擺渡人</a> 
            </li>
            <li>
                <a href="img/城市.png" title="洛陽城" >洛陽城</a>
            </li>
            <li>
                <a href="img/守衛(wèi).jpg" title="地獄口" >地獄口</a>
            </li>
            <li>
                <a href="img/海怪.jpg" title="海上龍" >海上龍</a>
            </li>
        </ul>
        <p id="description">選擇一張圖片</p>
        <img id="placeholder" src="img/透明.png" alt="占位圖"/>

該例是要實現(xiàn)點擊鏈接,在當前頁面打開圖片。
本來,寫成:

links[i].onclick=function()
{
    showpic(this);
    return false;
}

是沒問題了,可是按照書上改成return !showpic(this);
就無法實現(xiàn)在當前頁面打開
請問錯誤在哪里?
多謝多謝多謝回答O(∩_∩)O

回答
編輯回答
孤星

!showpic(this)返回的是true

2017年6月19日 02:48