I have a jQuery onClick
handler, writed with an anonymous function like that:
$("#selector").on("click" function(){
// do something
})
I would generalize the anonymous function extracting the logic in a named function, that drive me to something like:
$("#selector").on("click" namedFunction())
function namedFunction(){
// do something
}
To me seemed a good solution. But there's a drawback since the namedFunction
is executed as soon script is loaded. Here you can test the bad behaviour.
Just pass the reference
of that function itself.
Try,
$("#selector").on("click", namedFunction);
You don't need the ()
for your function here:
$("#selector").on("click", namedFunction)
try like
function namedFunction(){
alert("Hello world!")
}
$("#clickTester").on('click', namedFunction)
来源:https://stackoverflow.com/questions/22220216/jquery-on-click-event-that-call-a-named-function