jquery on click event that call a named function

后端 未结 6 587
青春惊慌失措
青春惊慌失措 2020-12-30 12:10

I have a jQuery onClick handler, writed with an anonymous function like that:

$(\"#selector\").on(\"click\" function(){
    // do something
})
<         


        
相关标签:
6条回答
  • 2020-12-30 12:24

    Named function[view jsFiddle]

    Function namedFunction () {
        alert("Hello world!");
    }
    $("#clickTester").on('click', namedFunction);
    

    Anonymous function [view jsFiddle]

    $("#clickTester").click(function(){
       alert("Hello world!");
    });
    
    0 讨论(0)
  • 2020-12-30 12:26

    To pass custom parameters to namedFunction use:

    $("#selector").on("click", null, {param1: param1}, namedFunction);
    
    
    namedFunction(event) { console.log(event.data.param1); }
    
    0 讨论(0)
  • 2020-12-30 12:35

    try like

    function namedFunction(){
    alert("Hello world!")
    }
    $("#clickTester").on('click', namedFunction)
    

    Updated Fiddle

    0 讨论(0)
  • 2020-12-30 12:40

    Just pass the reference of that function itself.

    Try,

    $("#selector").on("click", namedFunction);
    
    0 讨论(0)
  • 2020-12-30 12:44

    Or a shorter version

    $("#selector").click(namedFunction);
    
    0 讨论(0)
  • 2020-12-30 12:46

    You don't need the () for your function here:

    $("#selector").on("click", namedFunction)
    
    0 讨论(0)
提交回复
热议问题