开发者

jQuery click function called as soon as page loads

开发者 https://www.devze.com 2023-03-16 14:25 出处:网络
I have a jQuery script that basically is changing my css when I click a link: jQuery(\"a.adder\").click(css_change());

I have a jQuery script that basically is changing my css when I click a link:

jQuery("a.adder").click(css_change());

My problem is that when I first load the page, the css_change() is being fired even when the link hasn't been clicked. I know this for sure cause I changed the css_change() into alert("here") and that开发者_StackOverflow alert showed up. Anyone have any ideas as to why this is happening?


css_change()

Is immediately executing the function upon binding.

Use:

jQuery("a.adder").click(css_change);

(pass a reference to the function instead of the results of executing the function).


Remove the parenthesis on the function call.

jQuery("a.adder").click(css_change)
0

精彩评论

暂无评论...
验证码 换一张
取 消