2012-12-05 21 views
0

我有一些動態生成的html代碼:想不通的。每個函數使用哪個選擇

<div id="slider-7238" class="hps-slide" style="width:250px; height:50px;"> 
    <div class="hps-sliderText"> 
     Custom Pizza<br /> 
     Left: Mushrooms/Green Peppers<br/>Right: Cheese<span class="slide-more" id="slide-more-7238"> MORE... </span> 
      <div class="slide-full-desc" id="slide-desc-7238">Left: Mushrooms/Green Peppers<br/>Right: Ultimate Cheese</div>       
    </div> 
</div> 

<div id="slider-3471" class="hps-slide" style="width:250px; height:50px;"> 
    <div class="hps-sliderText"> 
     Chicken Alfredo<br /> 
     Grilled chicken breast strips and rotini pasta oven-baked<span class="slide-more" id="slide-more-3471"> MORE... </span> 
      <div class="slide-full-desc" id="slide-desc-3471">Grilled chicken breast strips and rotini pasta oven-baked in a creamy Alfredo sauce with a layer of melted cheese. Served with an order of breadsticks.</div>          
    </div> 
</div> 

<div id="slider-7261" class="hps-slide" style="width:250px; height:50px;"> 
    <div class="hps-sliderText"> 
     Custom Pizza<br /> 
     Left: Ham<br/>Right: Cheese 
    </div> 
</div> 

可以有任意數量的這些塊,以及與之相關的ID號永遠衆所周知。我有一個javascript函數,將slide-more跨度轉換爲鼠標懸停工具提示,slide-full-desc div的內容作爲工具提示內容。如果我在要使用的div ID中使用硬編碼,該功能可以正常工作,但由於動態生成,我無法做到這一點。我試圖在.each函數中使用它,但似乎無法使選擇器正確。我沒有得到任何錯誤;它只是不是所有的工作。

下面的代碼,因爲它主張:(。在這種情況下,只是佈局)

$(document).ready(function() { 
     $('span.slide-more').each(function (i) { 
      new QoTooltip($(this), $('slide-full-desc').html(), {'layout':'top'}); 
     }); 
    }); 

爲QoTooltip的PARAMS是鼠標懸停的元素,包含提示內容的元素,以及一些設置

我需要的是讓函數對每個跨度應用一個新的QoTooltip並傳遞相關的slide-full-desc div的內容。他們與他們的ID相關的四位數字(即span id="slide-more-3471"將需要得到slide-desc-3471 div的html。

鼠標懸停的作品,所以它至少得到.each的跨度,但我'不知道如何才能得到正確的內容進入第二PARAM。

回答

1

試試這個

$(this).next('.slide-full-desc').html(); 

完整的代碼

$(document).ready(function() { 
     $('span.slide-more').each(function (i) { 
      var $this = $(this); 
      new QoTooltip($this, $this.next('.slide-full-desc').html() 
                  , {'layout':'top'}); 
     }); 
}); 
+0

謝謝,這工作! – EmmyS

+0

@EmmyS ..不客氣:) –

1

嘗試像這樣

$('span.slide-more').each(function (i) { 
    new QoTooltip($(this), $(this).next('.slide-full-desc').html(), {'layout':'top'}); 
}); 
+0

接受舒尚特的回答不僅是因爲我先看見的。你的作品也是。 – EmmyS

相關問題