2010-05-24 17 views
6

से पहले तत्व डालें तत्व मैं ब्लॉक तत्व डालने की कोशिश कर रहा हूं किसी दूसरे से पहले कुछ डालें। मुझे यकीन नहीं है कि क्या मैं सही विधि का उपयोग कर रहा हूं लेकिन यहां मेरा कोड है। आशा है कि आप लोग मदद कर सकते हैं। धन्यवाद!Jquery <tr>

Jquery

$("#addMatch").click(function(){ 

    $("<td>New insert</td>").insertBefore("#addMatch").closest('tr'); 

    return false; //this would insert the <td>New insert</td> before the    
        //<td><input type="button" id="addMatch" name="addMatch" value="Add 
        //Match" </td> but not <tr> 
}); 

एचटीएमएल

<tr> 
<td>some data</td> 
</tr> 

//can't tell how many tr would show before the last "addMatch" button. It's dynamic. 
// I want the <td>New insert</td> show up here. 
    <tr> 
    <td><input type="button" id="addMatch" name="addMatch" value="Add Match" </td> 
    </tr> 

उत्तर

13

<td> हमेशा एक <tr> में होना चाहिए। इसके बजाय `.parents की

$("#addMatch").click(function(){ 
    $(this).parents('tr:first').before('<tr><td>New Insert</td></tr>'); 
    return false; 
}); 
+2

('टीआर: पहले')

मैं शायद इस तरह अपने कार्य होगा', वहाँ '.closest ('tr')' :) –

+0

.closest तेजी से होता है ? –

+0

अच्छा एक। धन्यवाद! हाँ, मुझे लगता है कि निकटतम तेज़ है। – FlyingCat

संबंधित मुद्दे