简体   繁体   中英

how to call my Java Script function correctly?

i just started learning JS i cant find what is wrong with my code:

<!DOCTYPE html>
<html>
    <body>
        <script>
        myfunc(){
            var no1=parseInt(document.getElementById("n1").value);
            var no2=parseInt(document.getElementById("n2").value);
            document.getElementById("f").innerHTML=no1+no2;
            }
        </script>
        num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/>
        <button onclick="myfunc()">=</button>
        <p id="f"></p>
    </body>
</html>

Just declare your function using the function reserved word

<!DOCTYPE html>
<html>
    <body>
        <script>
        function myfunc(){
            var no1=parseInt(document.getElementById("n1").value);
            var no2=parseInt(document.getElementById("n2").value);
            document.getElementById("f").innerHTML=no1+no2;
            }
        </script>
        num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/>
        <button onclick="myfunc()">=</button>
        <p id="f"></p>
    </body>
</html>

 <html> <body> <script> function myfunc(){ var no1=parseInt(document.getElementById("n1").value); var no2=parseInt(document.getElementById("n2").value); document.getElementById("f").innerHTML=no1+no2; } </script> num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/> <button onclick="myfunc()">=</button> <p id="f"></p> </body> </html>

Hey there. You need to use the word function before myfunc() to define this function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM