-1

這個簡單的test page是爲了顯示一個鏈接,並在用戶點擊時彈出警告。可防止onclick事件處理程序運行?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head> 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"> 

     <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script> 

    <!-- uncommenting the next line prevents the alert from showing up --> 
    <!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"> --> 

     <script type="text/javascript"> 

     $(document).ready(function() { 

     $("#show_more_link").click(function(e) { 
      alert("on click running"); 
      e.preventDefault(); 
     }); 
    }); 
    </script> 
</head> 

<body> 
    <a id="show_more_link" href="#">click here</a> 
</body> 

</html> 

問題:在取消其中包括引導的精縮的JavaScript行(第10行)打破了onclick事件:

  • 如果該行被註釋掉,點擊鏈接彈出一個警告(如預期)。

  • 如果該行未註釋,則單擊該鏈接將不執行任何操作。

這是怎麼發生的?

回答

2
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"> 

您的腳本未關閉/缺少結束</script>標籤,這就是爲什麼你會得到錯誤以及代碼休息,你需要做的是這樣的:

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
相關問題