单击关闭输入但不关闭页面时,JQuery清除div

时间:2016-07-20 18:30:27

标签: javascript jquery

我正在尝试在用户点击或标记输入时清除div,但如果用户更改为其他窗口或浏览器标签,我不希望div被清除。

到目前为止,我在两种情况下都清除了div:

$input.on('blur focus mousedown', function () {
  $input.val("");
  $("#cors-test-invalid").html("");
  $("#cors-test-views").html("");
  $("#cors-test-workbooks").html("");
  $("#cors-test-datasources").html("");
  $("#cors-test-projects").html("");
  $("#cors-test-users").html("");
});

1 个答案:

答案 0 :(得分:1)

你可以这样做

$(document.body).on('click',function(){
//here its a click on body which eliminates the other tabs or windows
if(!$input.is(":focus"))
{
//here element doesnt have focus, so we are good to do the logic
  $input.val("");
  $("#cors-test-invalid").html("");
  $("#cors-test-views").html("");
  $("#cors-test-workbooks").html("");
  $("#cors-test-datasources").html("");
  $("#cors-test-projects").html("");
  $("#cors-test-users").html("");

}

});
相关问题