2010-10-13 9 views
6

मैं एक मेज मैं उस के साथ काम कर रहा हूँ है इस तरह की तरह है:jQuery का उपयोग करते हुए प्रत्येक तालिका पंक्ति पर पहले टीडी के लिए वर्ग को जोड़ने के लिए

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tablecontent"> 
    <tr class="tablerow"> 
    <td>This is the td I want to add a class to.</td> 
    <td class="cell2">Stuff</td> 
    <td class="cell3">Stuff</td> 
    </tr> 
    <tr class="tablerow"> 
    <td>This is the td I want to add a class to.</td> 
    <td class="cell2">Stuff</td> 
    <td class="cell3">Stuff</td> 
    </tr> 
</table> 

प्रत्येक पंक्ति में पहले टीडी टैग कक्षा या आईडी नहीं है काम साथ में करने केलिए। मेरे पास HTML आउटपुट को बदलने की पहुंच नहीं है इसलिए मुझे प्रत्येक टैबलेरो के पहले टीडी टैग को लक्षित करने के लिए थोड़ा सा jQuery जोड़ने के लिए लगा। यह मैं कैसे करूंगा?

उत्तर

11
$('#tablecontent td:first-child').addClass('someClass'); 

यह the first-child selector का उपयोग करता है अपने माता पिता की एक first-child हैं #tablecontent तालिका में सभी <td> तत्वों का चयन करने के ।

उदाहरण:http://jsfiddle.net/duKKC/

+0

बहुत बढ़िया। यह पूरी तरह से काम किया। धन्यवाद! –

+0

@Robert - आपका स्वागत है। : ओ) – user113716

1

आप jQuery

नीचे की कोशिश कर सकते
$('.tablerow').each(function(index) { 
    $(this).children('td').first().addClass('class'); 
}); 

यह आपकी समस्या का समाधान होगा :)

0
$(".tablerow").find("td:first-child").addClass('class'); 
0

मेरा मानना ​​है कि निम्नलिखित काम करेगा:

$('.tablerow td:first-child').addClass('class'); 
0
$('#tablecontent td:first-child').addClass("first-row");