2013-02-28 91 views
-2

我有例如下面的代碼:jQuery的更改鏈接與圖片

<table class="catsTable" width="100%" cellspacing="1" cellpadding="0" border="0"> 
    <tbody><tr> 
    <td style="width:25%" class="catsTd" id="cid5" valign="top"> 
     <a href="/load/prison_break_online/sezonul_1/5" >Sezonul 1</a> <span class="catNumData" style="unicode-bidi:embed;">[6]</span> 
    </td> 
    <td style="width:25%" class="catsTd" id="cid6" valign="top"><a href="/prison_break_online/sezonul_2/6" class="catName">Sezonul 2</a> <span class="catNumData" style="unicode-bidi:embed;">[0]</span></td> 
    <td style="width:25%" class="catsTd" id="cid7" valign="top"><a href="/prison_break_online/sezonul_3/7" class="catName">Sezonul 3</a> <span class="catNumData" style="unicode-bidi:embed;">[0]</span></td> 
    <td style="width:25%" class="catsTd" id="cid8" valign="top"><a href="/prison_break_online/sezonul_4/8" class="catName">Sezonul 4</a> <span class="catNumData" style="unicode-bidi:embed;">[0]</span></td> 
    </tr></tbody> 
</table> 

,我想改變這種:

<a href="/load/prison_break_online/sezonul_1/5" >Sezonul 1</a> <span class="catNumData" style="unicode-bidi:embed;">[6]</span> 

成類似:

<img src="/images/pris5.png"> <br> 
    <a href="/load/prison_break_online/sezonul_1/5" >Sezonul 1</a> <span class="catNumData" style="unicode-bidi:embed;">[6]</span> 

pris5.png ==來自系列prison_break_online(pris)的前4個字母以及系列號 sezonul_1/5(5)..的ID t ID由下一個公式=> ..如果從URL的最後2個字符< 10 =>從url獲得最後一個字符否則獲得最後2個字符從url)

回答

0

有幾種方法可以做到這一點,但here is one

一個簡單的jQuery函數通過類名抓取每個TD,然後解析href屬性。

$(function(){ 
    $(".catsTd").each(function(index) { 
     var tmp1 = $(this).children("a").attr('href').split('/'); 
     $(this).prepend("<img src='/images/" + tmp1[1].substring(0, 4) + tmp1[3] + ".png'> <br>")  
    }); 
}); 

您的原始HTML格式化:

<table class="catsTable" width="100%" cellspacing="1" cellpadding="0" border="0"> 
    <tbody><tr> 
    <td style="width:25%" class="catsTd" id="cid5" valign="top"><a href="/prison_break_online/sezonul_1/5" class="catName">Sezonul 1</a> <span class="catNumData" style="unicode-bidi:embed;">[0]</span></td> 
    <td style="width:25%" class="catsTd" id="cid6" valign="top"><a href="/prison_break_online/sezonul_2/6" class="catName">Sezonul 2</a> <span class="catNumData" style="unicode-bidi:embed;">[0]</span></td> 
    <td style="width:25%" class="catsTd" id="cid7" valign="top"><a href="/prison_break_online/sezonul_3/7" class="catName">Sezonul 3</a> <span class="catNumData" style="unicode-bidi:embed;">[0]</span></td> 
    <td style="width:25%" class="catsTd" id="cid8" valign="top"><a href="/prison_break_online/sezonul_4/8" class="catName">Sezonul 4</a> <span class="catNumData" style="unicode-bidi:embed;">[0]</span></td> 
    </tr></tbody> 
</table> 
+0

TXN ..看起來像你的榜樣,但這裏的工作原理:http://www.ufilme.ro/load?lista_seriale_online?t1所有圖像:「 /seriale/images/gr2/load.png「 – 2013-02-28 18:39:52

+0

使用調試器查看在'/'上分割時創建的數組。你的例子與你的實際代碼完全不同。創建圖像字符串時,您需要使用不同的索引。我不知道你想要指的是什麼形象,但嘗試這樣:'$(this).prepend(「
」);'' – davehale23 2013-02-28 19:38:11