2017-05-02 6 views
1

私は外でクリックするとコンテンツが消えるツールチップを作成しようとしていますが、異なる投稿への解決策を試してみました。jQuery Blur()がclick()関数と連携していません

問題は私がツールチップをどのように起動しているかと思いますが、私が知っている唯一の方法です。ここに私のjsと私のHTMLマークアップのコードがあります。

ツールチップがうまくいきますが、私が試した方法ではblurイベントでは機能しません。

$(document).ready(function() { 

    function tooltover(target_item){ 
     $(target_item + '> a').click(function(){ 
      $('.tiptover_box').focus(); 

      var pos = $(this).position(); 
      var width = $(this).outerWidth(); 
      var boxw = $(this).next().outerWidth(); 
      var boxh = $(this).next().outerHeight(); 

      $(this).next().css('left', (pos.left - ((boxw/2)-(width/2)))); 
      $(this).next().css('top', (pos.top - (boxh+5))); 
      $(this).next().addClass('tiptover_show'); 
      $(this).next().delay(5).queue(function(){ 
       $(this).addClass('tt-in'); 
       $(this).dequeue(); 
      }); 
     }); 
     $('.tiptover_box').blur(function(){ 
      $('.tiptover_box').removeClass('tiptover_show tt-in'); 
     }); 
    } tooltover('.tiptover'); 

}); 

そしてHTML

<div class="tiptover"> 
    <a>Link Test</a> 
    <div class="tiptover_box" style="background-image: url(content/prod_0002.jpg);"> 
     <div>Description.</div> 
    </div> 
</div> 

答えて

1

異なるアプローチは、div自体に集中するのではなく、DOMの残りの部分に焦点を合わせることです。 this solution

彼はdocument自体にmouseupイベントを使用し同様の質問にprc322によって与えられた:今、あなたのツールチップは、「閉じられます

$(document).mouseup(function (e) 
{ 
    var container = $(".tiptover_box"); 

    if (!container.is(e.target) // if the target of the click isn't the container... 
     && container.has(e.target).length === 0) // ... nor a descendant of the container 
    { 
     container.removeClass('tiptover_show tt-in'); 
    } 
}); 

(関連性のために私が編集)

.tiptover_boxまたはその子孫でない要素にmouseupイベントが発生したときに発生します。

+1

良いニュース、これはちょうど期待どおりに機能しました。どうもありがとうございました。私は、何かが単なるぼかしよりも信頼性が高いと感じていました。いくつかのCSSクラスを追加および削除するために、いくつかの小さな機能を追加します。再度、感謝します! :D –

0

の代わりに(入力フィールドをぼかすほど信頼性が高くありません)のdivのblurイベントをトリガしようとしている、あなたはあなたがしたいと何ができます、からの回答と基本的に同じコンテナとその子のための追加のテスト:

$('body').click(function(e){ 
     var tooltipContainer = $('.tiptover_box'); 
     var clickTarget = e.target; 

     if(!tooltipContainer.is(clickTarget) 
      && tooltipContainer.has(clickTarget).length === 0){ 
      tooltipContainer.removeClass('tiptover_show tt-in'); 
     } 
}); 

編集: - 何がクリックされたとき、あなたはツールチップを隠します ボブ。

+0

なぜそれが信頼できないのですか? –

+1

@Bob - divsは 'onfocusout'イベントを発生させません。 – DigiFriend

+0

ありがとう、私の友人、これは動作しませんでした、私はテストし、ツールチップは発砲しなかった。実際には、ツールチップ以外の他の機能に少し問題があり、ブートストラップのクリックと競合する可能性があります –

関連する問題