鍍金池/ 問答/HTML/ js中原有函數(shù)增添新語句后原先語句功能也一并失效,是為什么呢?

js中原有函數(shù)增添新語句后原先語句功能也一并失效,是為什么呢?

<!DOCTYPE html>
<html>
<head>
    <title>test4.4</title>
    
    <script type="text/javascript" src="D:\draft\primaryJavascript\test44.js"></script>
</head>
<body>

<h1>Quagmire</h1>
<ul>
    <li>
        <a href="D:\draft\primaryJavascript\fireworks.jpg" onclick="showPig(this); return false;" title="a fireworks display">BoomShakaLaka</a>
    </li>
    <li>
        <a href="D:\draft\primaryJavascript\coffee.jpg"  title="a cup of black coffee" onclick="showPig(this);return false;">WannaDie?</a>
    </li>
    <li>
        <a href="D:\draft\primaryJavascript\BigBen.jpg" onclick="showPig(this);return false;" title="the famous clock">the giant clock is going to hit you directly.</a>
    </li>
    <img id="placeholder" src="D:\draft\primaryJavascript\placeholder.jpg"/ alt="my image gallery"/>
</ul>
<p id="description">Choose an image.</p>
</body>
</html>

js代碼

function showPig(whichpig){
    var source=whichpig.getAttribute("href");
    var placeholder=document.getElementById("placeholder");
    placeholder.setAttribute("src",source);
}

js代碼新增3條語句后 原先點(diǎn)擊圖片鏈接停留在當(dāng)前頁面和placeholder轉(zhuǎn)為鏈接圖片功能失效

function showPig(whichpig){
    var source=whichpig.getAttribute("href");
    var placeholder=document.getElementById("placeholder");
    placeholder.setAttribute("src",source);
    **var text=whichpig.getAttribute("title");
    var description=getElementById("description");
    description.firstChild.nodeValue=text;**
}

有趣的是,我對新增的三條語句逐條注釋看看是哪條語句使得所有函數(shù)功能失效。是這一句:
var description=getElementById("description");
我使用的編輯器是sublime text3.
那么如何才能實(shí)現(xiàn)所有功能呢?

回答
編輯回答
裸橙

應(yīng)該是var description=document.getElementById("description");這樣吧

2017年9月16日 02:16
編輯回答
兔寶寶

是因?yàn)槟闾砑拥恼Z句出錯,而JS引擎對錯誤的處理方式就是拋出錯誤并且程序中斷在你出錯的地方,導(dǎo)致后面的語句沒有執(zhí)行。

2018年5月24日 11:48