jQuery:在窗体输入焦点,显示div。隐藏div模糊(与警告) [英] jQuery: On form input focus, show div. hide div on blur (with a caveat)

查看:175
本文介绍了jQuery:在窗体输入焦点,显示div。隐藏div模糊(与警告)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ c $ 

使用下面的代码,我可以制作隐藏的div显示/隐藏(函数){
'('div.example')。css('display','block');
})。blur(function (){
$('div.example')。fadeOut('medium');
});

问题是我想 div.example 当用户在

内进行交互时继续显示。例如。点击或突出显示 div.example 中的文字等。但是,只要输入不在焦点,鼠标与div内的元素交互, div.example 就会淡出。

<输入和div元素的html代码如下:

 < p> 
< label for =example>文字:< / label>
< input id =examplename =exampletype =textmaxlength =100/>
< div class =example>一些文字...< br />< br />更多文字...< / div>
< / p>

如何使 div.example div.example 时,$ c>会消失。我尝试着用focusin / focusout来检查< p> 的焦点,但是也没有成功。



<使用jQuery将 div.example 直接放在输入字段#example下面吗?这样做的代码如下:

  var fieldExample = $('#example'); 
$('div.example')。css(position,absolute);
$('div.example')。css(left,fieldExample.offset()。left);
$('div.example')。css(top,fieldExample.offset()。top + fieldExample.outerHeight());

我很抱歉,如果之前已经问过这个问题,但是我读的很多show / hide div问题掩盖这一点。谢谢你的建议。 :)

解决方案

如果您跟踪文档中的focusin事件,那么您可以计算出新焦点是外部,如果是这样的话。 (b)
$ b

  $('#example')。focus(function(){$ b $ ($)$($)$($)$($)$($) ('。example'); $ b $ div.fadeOut('medium'); 
});
});
$('div.example')。hide();

更新了代码同时使用focusin和click事件来决定是否隐藏div.example。我使用命名空间事件,以便我可以调用unbind('。example')来解除它们的绑定。


$ b 示例: http://jsfiddle.net/9XmVT/11/


改变你的css定位代码只调用一次css方法:

  $('div。例子')。css({$ b $position:absolute,
left:fieldExample.offset()。left,
top:fieldExample.offset()。 + fieldExample.outerHeight()
});

使用绝对定位div的示例: http://jsfiddle.net/9XmVT/14/


$ b

更新



Ben Alman刚刚更新了他的点击事件并将其转换为处理大量外部事件。 http://benalman.com/projects/jquery-outside-events-plugin/



会让你做这样的事情:

  $ ('#example')。focus(function(){
$('div.example')。show()。bind('focusoutside clickoutside',function(e){
$(this) ('mediumout');
});
});
$('div.example')。hide();


I am able to make a hidden div show/hide when an input field is in focus/blur using the following code:

  $('#example').focus(function() {
    $('div.example').css('display','block');
  }).blur(function() {
    $('div.example').fadeOut('medium');
  });

The problem is I want div.example to continue to be visible when the user is interacting within this div. E.g. click, or highlighting the text etc. within div.example. However div.example fades out whenever the input is not in focus and the mouse is interacting with elements within the div.

The html code for the input and div elements is as follows:

<p>
<label for="example">Text:</label>
<input id="example" name="example" type="text" maxlength="100" />
<div class="example">Some text...<br /><br />More text...</div>
</p>

How do I make it such that the div.example only disappears when the user clicks outside the input and/or div.example? I tried experimenting with focusin/focusout to check the focus on <p> but that didn't work either.

Would it matter that div.example is positioned directly below the input field #example using jQuery? The code that does that is as follows:

var fieldExample = $('#example');
$('div.example').css("position","absolute");
$('div.example').css("left", fieldExample.offset().left);
$('div.example').css("top", fieldExample.offset().top + fieldExample.outerHeight());

My apologies if this has been asked before, but the many show/hide div questions I read does not cover this. Thanks for your advice. :)

解决方案

If you track the focusin event on the document since focusin bubbles then you can figure out if the new thing in focus is "outside" and if so do something about it. This will work for both clicks and tabbing.

$('#example').focus(function() {
    var div = $('div.example').show();
    $(document).bind('focusin.example click.example',function(e) {
        if ($(e.target).closest('.example, #example').length) return;
        $(document).unbind('.example');
        div.fadeOut('medium');
    });
});
$('div.example').hide();​

Updated the code to use both the focusin and click event to decide if to hide the div.example. I am using namespaced events so that I can call unbind('.example') to unbind both of them.

Example: http://jsfiddle.net/9XmVT/11/

Side Note Change your css positioning code to only call the css method once:

$('div.example').css({
    "position":"absolute",
    "left": fieldExample.offset().left,
    "top": fieldExample.offset().top + fieldExample.outerHeight()
});

Example with using the absolute positioned div: http://jsfiddle.net/9XmVT/14/

UPDATE

Ben Alman just updated his clickoutside event and converted it to handle alot of *outside events. http://benalman.com/projects/jquery-outside-events-plugin/

Would let you do something like this:

$('#example').focus(function() {
    $('div.example').show().bind('focusoutside clickoutside',function(e) {
        $(this).unbind('focusoutside clickoutside').fadeOut('medium');
    });
});
$('div.example').hide();

这篇关于jQuery:在窗体输入焦点,显示div。隐藏div模糊(与警告)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆