jquery,单击页面上的空白区域时焦点将消失

时间:2016-06-24 03:10:48

标签: javascript jquery

单击空白区域时,

项目的focus()将消失。

代码喜欢

$("#cusArea").find("#" + cusId).focus();

我希望如果项目没有改变,无论点击空白区域还是右键点击,重点都会集中在当前项目上。

如何解决此问题?

2 个答案:

答案 0 :(得分:1)

这是事件的默认行为。但也有一些解决方法,有一些副作用,你必须要研究。

例如,如果您的目的是继续关注某些输入并希望保持焦点,直到输入一些值,那么我们就可以使用。

$("body").click(function(e){

if(!$("#inp1").val()){
 e.preventDefault();
   $("#inp1").focus();
  return;
}

希望你能对此做更多的试验,并对事件有所了解。

jsbin code sample

答案 1 :(得分:0)

demo

应该有效

var _value = null; 

//需要在函数包装器

之外声明它
$(function () {
    // Handler for .ready() called.

    _value = $('#test').val();
     //save start value of input

    $('#test').blur(function (e) {
        //catching blur event of input
        var _this = $(this);
        //saving jq object of element

        //compering stored value to current input value
        if (_value == _this.val()) {
            //values are same
            e.preventDefault();
            //set fosuc back on element
            _this.focus();
        } else {
            //in other case 
            // save new value
            // not sure i you need this
            _value = _this.val();
        }

    })
//set focus on input at the start
    $('#test').focus();
})