2016-10-14 118 views
0

爲什麼不能正常工作?無法通過onclick屬性調用函數

HTML:

<html> 
<head> 
    <title>test</title> 
</head> 
<body> 
    <button onclick="test21()">test</button> 
</body> 
</html> 

的javascript:

function test21(){ 
    console.log("yey"); 
} 
+0

檢查您是否正確包含了js https://plnkr.co/edit/Q0NnFQoWL5kEp2BsglrT?p=preview – Karim

回答

0

您可以創建調用這兩個的單一功能,然後在事件中使用它。

function myFunction(){ 
    pay(); 
    cls(); 
} 

,然後爲按鈕:

<input id="btn" type="button" value="click" onclick="myFunction();"/> 
0

您沒有在html文件中包含腳本。

<html> 
 
<head> 
 
    <title>test</title> 
 
    <script> 
 
    function test21(){ 
 
     console.log("yey"); 
 
     } 
 
    </script> 
 
</head> 
 
<body> 
 
    <button onclick="test21()">test</button> 
 
</body> 
 
</html>

上面的代碼片段將會運行得很好。
使用檢查器(ctrl + shift + I)啓動控制檯並單擊按鈕以獲得結果。

相關問題