仅当两个div不在焦点时如何激活Onblur JQUERY:Javascript [英] How to activate Onblur only if two divs are out of focus JQUERY:Javascript

查看:77
本文介绍了仅当两个div不在焦点时如何激活Onblur JQUERY:Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动建议

我有一个div作为输入标签,另一个有建议div ...我希望建议div在任何一个之后都消失

i have one div for the input tag and another for the suggestions div...i want the suggestions div to disappear after either

1)在建议div中选择了一个元素

1) an element is selected in the suggestions div

2)单击两个div之外的区域

2) when an area outside the two divs is clicked

问题是,我现在在哪里添加"onblur"事件

The question is where to add an "onblur" event right now i have

<input type = "text" name= "target" value = "" id="target" style="width:150px" onblur ="setTimeout('removeSuggestions()', 20);" onkeyup ="getSuggestions(this.value);"/>

<div id ="suggestions"></div>

function getSuggestions(value){
   if (value !=""){
   $.post("target.php", {targPart:value}, function(data) {
     $("#suggestions").html(data);
    if(value.length>2){
        doCSS();
        }

   });
   } else {
    removeSuggestions();
    }

  }

   function removeSuggestions(){

   $("#suggestions").html("");
   undoCSS();
   }
  function addText(value){

      $("#target").val(value);

  }
  function doCSS(){
  $("#suggestions").css({
      'border' : 'solid',
       'border-width': '1px'

    });

  }

  function undoCSS(){
   $("#suggestions").css({
      'border' : '',
       'border-width': ''

    });
  }

但是每次我尝试单击建议时,建议div都会消失,因为一旦我退出输入字段,就会调用删除建议.超时应该有帮助,但没有帮助.我该如何解决?

But everytime I try to click a suggestion the suggestions div goes away because once i go out of the input field remove suggestions is called. The timeout is supposed to help but isn't. How can i fix this?

推荐答案

也许类似的方法可以解决您的问题.我们只需要监视搜索框的模糊性,因为单击它(甚至在建议上)也会触发hide事件.

Perhaps something like this will solve your problem. We only need to monitor the blur of search box because clicking out of it (even on suggestions) will trigger the hide event.

http://jsfiddle.net/9Yt9L/3/

$('#search').focus(function() {
    $('#suggestions').slideDown();
});

$('#search').blur(function() {
    $('#suggestions').slideUp();
});

$('#suggestions div').click( function() {
    $('#search').val($(this).html());
});

这篇关于仅当两个div不在焦点时如何激活Onblur JQUERY:Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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