2013-07-17 44 views
0

http://codepen.io/leongaban/pen/quzyx如何獲得這個td的兄弟姐妹的img類名?

嗨,基本上就具體TD的選擇request - 行這jQuery的打了一個點擊事件。

我可以在tr上獲得與data-message-id關聯的編號,但是我很難找到正確的方法來定位第一個td中圖像上的類的名稱。


的HTML標記:

<table> 
<tr data-message-id="101010"> 
    <td class="table-icon request-row"> 
     <img class="accepted-icon" src="http://leongaban.com/_projects/whoat/_HTML/img/icon-orange-tick.png" alt="Accepted"/> 
    </td> 
    <td class="data-name request-row"> 
     Name 
    </td> 
    <td class="data-career request-row"> 
     Title 
    </td> 
    <td class="data-company request-row"> 
     Company 
    </td> 
    <td>Some image goes here<!--Not clickable--></td> 
</tr> 
</table> 

我的jQuery

$(".request-row").unbind('click').bind('click', function() { 
    alert('clicked .request-row'); 

    var $tr = $(this).closest("tr"); 
    var id = $tr.data('message-id'); 
    msg_id_incoming = id; 
    var userType = $tr.data('type'); 

    // if accepted then change up the content displayed... 
    //var $status = $(this).parent.children('img').attr('class'); 
    var $status = $(this).siblings('img').attr('class'); 
    alert($status); 

}); 


你如何重新寫// if accepted then change up the content displayed...註釋下的代碼,以獲得該當點擊類名爲request-row的td時,圖像上的類名稱是什麼?

我Codepen:http://codepen.io/leongaban/pen/quzyx

+0

你能簡單地添加一個總是在那裏的類,然後檢查其他類是否存在? – amaster

回答

4

一種方法是:

$(this).siblings('td.table-icon').find('img').attr('class'); 

jsFiddle here.

+0

甜,謝謝! thx也爲jsFiddle –

0
var $status = $(this).siblings().first().children('img').attr('class'); 
0

因爲你的形象是在TD,爲此沒有的this silbling,你需要選擇包含圖像的兄弟姐妹然後得到圖像:

$(this).siblings('td:has(img)').children('img').attr('class')