JQuery 结合焦点和悬停事件
我在将 HOVER 和 FOCUS 事件与 jquery 结合起来时遇到了一些麻烦。这就是我最初的内容:
$("input,textarea").focus(function () {
$(this).parent().siblings('div.exp').removeClass('hide');
$(this).parent().siblings('div.exp').addClass('show');
});
$("input,textarea").blur(function (){
$(this).parent().siblings('div.exp').removeClass('show');
$(this).parent().siblings('div.exp').addClass('hide');
if ($(this).val().length <= 0) {
$(this).siblings('span.warning').removeClass('hide');
$(this).siblings('span.warning').addClass('show');}
else {
$(this).siblings('span.warning').removeClass('show');
$(this).siblings('span.warning').addClass('hide');
}
});
基本上,我有一个用户联系表单,其中包含如下所示的行:
<div class="row">
<p><label>Your Name</label><input type="text" name="name" id="name" value=""/><span class="warning">Your name is missing</span></p>
<div class="exp">Who am I to address?</div>
</div>
我的 Jquery 代码的要点是,当用户也聚焦任何一个输入或文本区域元素时,显示一个隐藏的 div (exp)当取消聚焦(模糊)元素时检查所述输入的值是否不为空。 (我还没有真正开始验证,所以现在检查字符串长度只是一个临时填充符)。如果元素的字符串小于或等于 0,则将向用户“显示”span.warning。
这一切都运行良好。 我陷入困境的地方如下:
我想添加悬停但不与焦点冲突。我想要的最终效果是这样的:
将鼠标悬停在任何输入或文本区域上,就会显示 div.exp (exp 用于解释)。当您聚焦任何输入或区域时,div.exp 会保留在那里,即使您将鼠标悬停在任何其他输入或文本区域上也是如此。如果您将鼠标悬停在已经聚焦的输入上,则不会发生任何事情。
因此,简而言之,焦点和悬停元素应该“独立”工作。不确定我是否说清楚了,但是哦,好吧,我尝试过=)
干杯 G
I am having a bit of trouble combining the HOVER and FOCUS events with jquery. This is what I had originally:
$("input,textarea").focus(function () {
$(this).parent().siblings('div.exp').removeClass('hide');
$(this).parent().siblings('div.exp').addClass('show');
});
$("input,textarea").blur(function (){
$(this).parent().siblings('div.exp').removeClass('show');
$(this).parent().siblings('div.exp').addClass('hide');
if ($(this).val().length <= 0) {
$(this).siblings('span.warning').removeClass('hide');
$(this).siblings('span.warning').addClass('show');}
else {
$(this).siblings('span.warning').removeClass('show');
$(this).siblings('span.warning').addClass('hide');
}
});
Basically, I have a user contact form with rows like the one below:
<div class="row">
<p><label>Your Name</label><input type="text" name="name" id="name" value=""/><span class="warning">Your name is missing</span></p>
<div class="exp">Who am I to address?</div>
</div>
The point of my Jquery code is to bring forth a hidden div (exp) when the user focuses any one input or textarea element as well as checking if the value of said input is not empty when unfocusing (blur) the element. (I haven't really gotten down to validation yet so checkin for the string length right now is just a temporary filler). Should the element have a string smaller or equal than 0, then span.warning is to be 'shown' to the user.
This is all working nicely.
Where I get stuck is the following:
I want to add in hover but without conflicting with focus. My desired final effect is this:
You hover any input or textarea and you get the div.exp to show up (exp is for explanation). You focus any input or area and the div.exp stays there, even if you go about hovering any other inputs or textareas. Should you hover an input that is already focused, nothing should happen.
So, in a nutshell, the focus and hover elements should work 'independently' so to speak. Not sure if I made myself clear but oh well, I tried =)
Cheers
G
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用
.hide()
和.show()
并链接事件,可以显着缩短您的代码。我在此处发布了演示。Your code can be significantly shortened by using
.hide()
and.show()
and chaining the events. I posted a demo here.您可以在输入或文本区域聚焦时为其设置一个标志,以避免与悬停事件发生冲突。如果在触发 over 或 out 事件时将该标志设置为 true,则不会执行其代码。下面的代码展示了这个想法(我没有测试过)。
You can set a flag to the input or textarea while it is focused to avoid conflict with your hover event. If the flag is set to true when the over or out event is fired, its code is not executed. The following code show the idea (I have not test it).