JavaScript function executing too soon

后端 未结 3 800
离开以前
离开以前 2020-12-22 13:28

In the example below, myFonk is called instantly; it doesn\'t wait for the click event. Why not?

    function myFonk(info) {
        $(\"#result         


        
3条回答
  •  半阙折子戏
    2020-12-22 14:26

    Whenever we write the function name with () it calls that function instantly hence myFonk("myFonk") is not correct way..

    Write in following manner.

    function myFonk(info) {
        $("#result").html(info);
    }
    
    function getText(text) {
        return function () {
            myFonk(text);
        }
    }
    
    $(document).ready(function () {
        $("#b1").click(getText("getText"));
        $("#b2").click(function () {
            myFonk("myFonk")
        });
    });
    

提交回复
热议问题