JavaScript Error: Uncaught TypeError: foo is not a function

后端 未结 3 735
半阙折子戏
半阙折子戏 2021-01-25 01:56

For some reason JavaScript doesn\'t seem to recognise my function.

I have a button in some HTML:

3条回答
  •  孤街浪徒
    2021-01-25 02:23

    Instead of using the onclick attribute, it is much cleaner to add the event listener in JavaScript, like so:

    $('#lightOn').on('click', function() {
      $("#lightOn").addClass("active");
      $("#lightOff").removeClass("active");
      socket.emit("setting", {"light":"on"});
    });
    
    
    
    

提交回复
热议问题