[英]Why focus event doesn't work correctly?
Here is my code:这是我的代码:
function removeWrongClass(el){ el.removeClass('wrong'); } $(".ask_q_title_input").on('focus', removeWrongClass($(this)));
.wrong{ background:red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="ask_q_title_input wrong" type="text" />
Expected behavior is removing that red background color (which is because of .wrong
class) from the input when you focus on the input.当您关注输入时,预期的行为是从输入中删除红色背景颜色(这是因为
.wrong
类) 。 But it doesn't happen.但它不会发生。 What's wrong?
怎么了? And how can I fix it?
我该如何解决?
As others have said, you are calling the function, instead of passing it as the function to run.正如其他人所说,您正在调用该函数,而不是将其作为要运行的函数传递。
function removeWrongClass(){ $(this).removeClass('wrong'); } function toggleWrongClass(){ $(this).toggleClass('wrong'); } $(".ask_q_title_input").on('focus', removeWrongClass); $(".ask_q_title_input_toggle").on('focus focusout', toggleWrongClass);
.wrong{ background:red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Turn off wrong on focus - <input class="ask_q_title_input wrong" type="text" /><br/> Toggle wrong on focus in and out - <input class="ask_q_title_input_toggle wrong" type="text" />
If you would like to use removeWrongClass
on another element you can call it as follows -如果您想在另一个元素上使用
removeWrongClass
,您可以按如下方式调用它 -
removeWrongClass.call(elementYouWantAsThis)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.