Addeventlistener with ajax not working well

前端 未结 2 781
花落未央
花落未央 2020-11-28 16:46

I want to show the content of a txt file inside a \'div\', so I\'m calling my function with a button, but the functions triggers even if I don\'t push the button, here\'s my

相关标签:
2条回答
  • 2020-11-28 17:15

    You are calling a function instead of passing in as a anonymous method.

    window.onload=function(){
        document.getElementById("showF").addEventListener("click" , function(){
            sacardatos('P1.txt','container');
        },false);
    }
    
    0 讨论(0)
  • 2020-11-28 17:29

    The second argument to addEventListener should be a function. You're not passing a function, you're calling the function immediately and passing the result. Change to:

    document.getElementById("showF").addEventListener("click", function() {
        sacardatos('P1.txt','container');
    }, false);
    
    0 讨论(0)
提交回复
热议问题