2010-05-24 3 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> 
}); 

HTML은

<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>에 있어야합니다.

아마 이런 식으로 함수를 만들 것 :

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

을 대신 .parents'의 ('TR : 첫 번째')','.closest ('TR') '있다 : –

+0

빠른 .closest입니다 ? –

+0

좋습니다. 감사! 네, 가장 가까운 것이 더 빠르다고 생각합니다. – FlyingCat

관련 문제