2017-02-28 17 views
0

對此的任何意見?當用戶按下按鈕時,最初隱藏的div必須顯示在頁面上。函數未定義爲我的簡單按鈕顯示div javascript

<!DOCTYPE html> 
    <html> 
    <head> 

    <style> 
     div { 
     display: none; 
     } 
    </style> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> 
     function show() { 
       $("div").toggle(); 
     } 
    </script> 

    </head> 


    <body> 
    <p>Animals are cute!</p> 
    <div><p>Some text gonna blow up some pixels!</p></div> 
    <button onclick="show()">Click</button> 

    </body> 

    </html> 

在控制檯中究竟寫的錯誤:

ReferenceError: show is not defined[Learn More 
+0

你**必須**定義/把腳本在頁面的底部。 –

+1

將函數放在另一個腳本標記中 –

回答

3

移動內嵌腳本到它自己的標籤。您不能在同一個標​​記中使用外部腳本和內聯腳本。

所以:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script> 
function show() { 
    $("div").toggle(); 
} 
</script> 
0

function show() { 
 
    $(".any-text").toggle(); 
 
}
.any-text { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 
    <body> 
 
      <p>Animals are cute!</p> 
 
      <div class="any-text"><p>Some text gonna blow up some pixels!</p></div> 
 
      <button onclick="show()">Click</button> 
 
    </body> 
 
</html>