2011-06-17 81 views
0

所以我有一堆<li>的網格格式。我有一些jQuery的設置讓你可以將鼠標懸停在li上,一組按鈕以不同的選項顯示在li上。我有jquery工作正常,直到我將hover選擇器從<li>中的元素移動到整個<li>。現在看來,當我試圖將我的按鈕添加到li到特定元素時,它似乎可以附加到每個<li>,而不僅僅是我徘徊在的那個.note上我試圖使用它來附加,並且這不起作用無論是什麼,而不是發生:jquery append問題

$.tmpl(quickbuttonTemplate).appendTo($(this).closest('.details'); 

,這樣的方式我沒有得到任何結果,所以我用這個去了,這是我在那裏有它追加到每立而不只是一個我一直在徘徊:

function quickView() { 
    var quickbuttonTemplate = $('#quick-button-template').template(); 
    var quicklightTemplate = $('#quick-view-template').template(); 
    // Grid Hover Overlays 
    $('.grid li').live('mouseover', function() { 
     if (!$(this).data('init')) { 
      $(this).data('init', true); 
      $(this).hoverIntent(

      function() { 
       $.tmpl(quickbuttonTemplate).appendTo($('.details')); 
       $('.quick-container').css({ 
        'width': $('.grid li').width() + "px", 
        'height': ($('.grid li').height() - 56) + "px" 
       }).show(); 
      }, 

      function() { 
       $('.quick-container').remove(); 
      }); 
      $(this).trigger('mouseover'); 
      interval: 20 
     } 
    }); 

這裏是我的html:

<li data="{ id: Some number }"> 
    <div class="header"> 
     <h4><a href="/some url" title="some text">Header name</a></h4> 
    </div> 
    <div class="details"> 
     <div class="photo"> 
      <a href="some url" title="some text"><img src="some image" alt="some text"></a> 

     </div> 
     <div class="logo"> 
      <a href="some url" title="some text"> 
       <img src="/some url" width="50" height="25" alt="some text" title="some text"> 
      </a> 
     </div> 
     <div class="pricing clearfix"> 
      <span class="price">some price</span> 
     </div>             
    </div> 
    <div class="rating clearfix"> 
     <a href="some url" title="some text"><div class="rating-4" onclick="window.location='some url"></div></a> 
     <a href="some url" title="some text" class="rating-count countLeft">29</a>          
    </div> 
</li> 
+1

問題是什麼? – Jon 2011-06-17 12:36:41

+1

如果您向我們顯示標記可能... – 2011-06-17 12:39:10

回答

1

這樣的:

$.tmpl(quickbuttonTemplate).appendTo($(this).closest('.details'); 

沒有B/C最近查找下樹,你想看看的$(本)內工作...

最接近的文檔: http://api.jquery.com/closest/

你可以使用.find

$.tmpl(quickbuttonTemplate).appendTo($(this).find('.details'); 

也:如果您在多個然後使用$(本) e行,就像你一樣,最好將它存儲在局部變量中。即:

var $this = $(this);