2010-03-02 78 views
0

我下面是對TD的HTML代碼,它獲取的ID從下面LI HTML代碼掠從ID爲相同小區的ID

<td style="border-top-style: solid; border-right-style: solid; border-left-style: solid; 
    border-bottom-style: solid" id="Communication"> 
    Communication Science Course 
</td> 

<li id="CommunicationTooltip"><a href="#" target="_blank" class="toolTip"> 
    <img src="/images/q_mark.gif" alt="" /><span style="width: 300px; padding: 10px 10px 10px 55px;">Testing 
    Communication.</span></a> 
</li> 

<td style="border-top-style: solid; border-right-style: solid; border-left-style: solid; 
    border-bottom-style: solid" id="Communication_1"> 
    Communication Science Course II 
</td> 

<li id="Communication_1Tooltip"><a href="#" target="_blank" class="toolTip"> 
    <img src="/images/q_mark.gif" alt="" /><span style="width: 300px; padding: 10px 10px 10px 55px;">Testing 
    Communication II.</span></a> 
</li> 

這裏匹配後追加爲jQuery的相匹配的李錨相對標識,並採取標籤從上面,並進一步追加在上述TD

$(document).ready(function() 
{ 

     // bind to cells with an ID attribute 
     $("table > tbody > tr > td[id]").each(function() 
     {     

      // grab the anchor from the LI whose ID starts with the cell's ID 
      var $tooltip = $("div:hidden li[id^=" + $(this).attr("id") + "] a"); 

      //alert($tooltip); 

      // append it to the current cell 
      $(this).append($tooltip); 

     }); 
}); 

,如果你看到我們有上面的HTML代碼兩種不同的LI的ID (通訊與Communication_1),問題是,如果我用Communication1作爲第二ID工作正常,但是當我使用Communication_1其追加錨標記通信 TD,如果你檢查上面的jquery它匹配的LI ID以相同的ID開頭,但是我希望它應該匹配完美的LI ID不以相同的ID開頭。

請修改上面的jquery代碼。

感謝

回答

1

我不知道你想達到什麼樣的100%,我只能猜測的問題是您正在使用「startwith」相匹配的ID。由於toolip ID始終是「idOfTDTooltip」,爲什麼不完全匹配它們呢?

var $tooltip = $("div:hidden li[id='" + $(this).attr("id") + "ToolTip'] a"); 

或者,不區分大小寫與ID +「提示」的ID進行比較:

$(document).ready(function() { 
    // bind to cells with an ID attribute 
    $("table > tbody > tr > td[id]").each(function() 
    {     
     appendTooltip($(this), $(this).attr('id')); 
    }); 
}); 

function appendTooltip(obj, theId) //find and append tooltip 
{ 
    $("div:hidden li[id^='" + theId + "'] a").each(function(){ 
    if($(this).parent().attr('id').toLowerCase() === (theId + "tooltip").toLowerCase()) { 
     obj.append($(this)); 
     return; 
    } 
    }); 
} 
+0

感謝您的答覆,但我的「工具提示」文本可以是小的情況下,我的意思是它可以也可以用「Tooltip」代替「ToolTip」,建議! – 2010-03-02 09:39:46

+0

@解決方案:我在我的代碼中添加了一種替代方法。沒有測試,但你會得到主意。我確定有更好的方法,只是沒有來找我:P – 2010-03-02 10:05:42

+0

嗨它沒有爲我工作它甚至沒有進入$ tooltip.each(函數(idx)函數,請建議 – 2010-03-02 10:24:00