防止模糊用户点击特定的div不工作在Firefox [英] Preventing blur when user clicks on specific div not working in Firefox

查看:141
本文介绍了防止模糊用户点击特定的div不工作在Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你点击一个特定的div时,我正在使用jquery保持文本框的焦点。它在Internet Explorer中运行良好,但不在Firefox中运行。有什么建议?

  var clickedDiv = false; ()函数(){如果(clickedDiv){$('input')。焦点();}}); 

$('输入')。
$ b $('div')。mousedown(function(){clickedDiv = true;})
.mouseup(function(){clickedDiv = false});


解决方案

jquery对象实际上并不关注它:它只是调用被调用的焦点处理程序!要实际上关注这个项目,你应该这样做:

  var clickedDiv = false; 
$('input')。blur(function(){
if(clickeddiv){
$('input')。each(function(){this [0] .focus )});


$('div')。mousedown(function(){clickedDiv = true;})
.mouseup(function(){clickedDiv =假的});

请注意,我在本机DOM对象上使用了focus jQuery的对象。

这是一个直接的(暴力)改变你的确切的代码。但是,如果我明白你想要做的正确的,你正试图集中一个输入框当一个特定的div被点击当这个输入焦点



这是我如何去做的: p>

  var inFocus = false; 
$('#myinput')。focus(function(){inFocus = true;} )
.blur(function(){inFocus = false;});
$('#mydiv')。mousedown(function(){$ b $ if(inFocus)
setTimeout (function(){$('#myinput')[0] .focus();},100) ;
}

注意:我已经给了一个超时,问题,这样投入实际上可能会同时偏离焦点。否则,我们会在即将失去它之前将其重点关注。至于100毫秒的决定,这真是一个侥幸。



干杯,

jrh






编辑回复@ Jim的评论

<第一种方法可能不起作用,因为这是错误的做法。



至于第二个问题,我们应该使用。因为原生的 .focus()方法会导致对象实际上抓住()方法,所以在本地DOM对象上不要使用焦点()焦点,而jquery方法只是调用与焦点事件关联的事件处理程序。



<因此,当jquery方法调用焦点事件处理程序时,本地方法实际上授予焦点,因此导致调用处理程序。这是一个不幸的命名,这种方法的名称重叠。


I am using jquery to keep the focus on a text box when you click on a specific div. It works well in Internet Explorer but not in Firefox. Any suggestions?

var clickedDiv = false;

$('input').blur(function() { if (clickedDiv) { $('input').focus(); } });

$('div').mousedown(function() { clickedDiv = true; })
        .mouseup(function() { clickedDiv = false });

解决方案

Point to note: the focus() method on a jquery object does not actually focus it: it just cases the focus handler to be invoked! to actually focus the item, you should do this:

var clickedDiv = false;
$('input').blur( function() {
    if(clickeddiv)  {
        $('input').each(function(){this[0].focus()});
    }
}
$('div').mousedown(function() { clickedDiv = true; })
        .mouseup(function() { clickedDiv = false });

Note that I've used the focus() method on native DOM objects, not jquery objects.

This is a direct (brute force) change to your exact code. However, if I understand what you are trying to do correctly, you are trying to focus an input box when a particular div is clicked when that input is in focus.

Here's my take on how you would do it:

var inFocus = false;
$('#myinput').focus(function() { inFocus = true; })
             .blur(function() { inFocus = false; });
$('#mydiv').mousedown(function() {
    if( inFocus )
        setTimeout( function(){ $('#myinput')[0].focus(); }, 100 );
}

Point to note: I've given a timeout to focussing the input in question, so that the input can actually go out of focus in the mean time. Otherwise we would be giving it focus just before it is about to lose it. As for the decision of 100 ms, its really a fluke here.

Cheers,

jrh


EDIT in response to @Jim's comment

The first method probably did not work because it was the wrong approach to start with.

As for the second question, we should use .focus() on the native DOM object and not on the jQuery wrapper around it because the native .focus() method causes the object to actually grab focus, while the jquery method just calls the event handler associated with the focus event.

So while the jquery method calls the focus event handler, the native method actually grants focus, hence causing the handler to be invoked. It is just unfortunate nomenclature that the name of this method overlaps.

这篇关于防止模糊用户点击特定的div不工作在Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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