2012-04-20 5 views
0

リストを含むツールヒントを作成しようとしていますが、このリストは選択可能と思われますので、次のwbepageのコードを使用しています:http://3nhanced.com/javascript/simple-tooltip-powered-by-jquery/ ツールチップを作成するのに、選択可能なアイテムを作成してください:jqueryセレクタが間違っていますか?

$(document).ready(function() { 

     $('.toolTip').hover(
      function() { 
       this.tip = this.title; 
       console.log($(this)); 
       $(this).append(
        '<div class="toolTipWrapper">' 
         +'<div class="toolTipTop"></div>' 
         +'<div class="toolTipMid">' 
          +'<ul id="selectable">' 
           +'<li class="ui-widget-content">Item</li>' 
           +'<li class="ui-widget-content">Item</li>' 
          +'</ul>'            
         +'</div>' 
         +'<div class="toolTipBtm"></div>' 
        +'</div>' 
       ); 
       this.title = ""; 
       this.width = $(this).width(); 
       $(this).find('.toolTipWrapper').css({left:this.width-22}) 
       $('.toolTipWrapper').fadeIn(300); 
      }, 
      function() { 
       $('.toolTipWrapper').fadeOut(100); 
       $(this).children().remove(); 
       this.title = this.tip; 
      } 
     ); 


     $("#selectable").selectable({ 
      stop: function() { 
       var $item2 = $(this), 
       $target = $(event.target); 
       console.log($target);     
       var result = $("#select-result").empty(); 
       $(".ui-selected", this).each(function() { 
        var index = $("#selectable li").index(this); 
        result.append(" #" + (index + 1)); 
       }); 
      } 
     });   
}); 

なぜそれが機能しないのでしょうか? 誰もが見ることができます。選択可能な関数を使用する前にツールチップの作成が最初に行われています。 ありがとうございます。

+0

トピックに少しばかり問題があります:あなたが何かを浮遊するたびにdivを作成/接続して破壊していますか? divが既に存在し、それを表示/非表示する方が簡単ではないでしょうか? –

答えて

1

要素が実際に存在する場合は$("#selectable").selectable(...)に電話する必要があります。つまり、ツールチップを作成した後です。

+0

$(document).onready funtcionの中に私のツールチップと選択可能な関数の作成があると、それは起こりませんか? – linker85

2

$(function() {...});のコードは、DOMの準備が整うと実行されます。ただし、.append()関数を使用して<ul id="selectable">を後で挿入しているため、コードの最初の部分が実行されたときに要素がに存在しません。

追加のHTMLを挿入したら、そのコードを実行する必要があります。

+0

$(document).ready関数内のすべてを実行しています – linker85

+0

@ linker85コードの各セクションを呼び出す順番に問題があるようです。 '$("#selectable ").selectable({...});セクションが** after ** **要素を追加する部分であることを確認してください。 –

+0

私は選択機能をホバー機能の中に入れて解決します。 – linker85

関連する問題