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>
6
задан FlyingCat 24 May 2010 в 23:36
поделиться

1 ответ

всегда должно быть в .

Я бы, вероятно, сделал вашу функцию такой:

$("#addMatch").click(function(){
    $(this).parents('tr:first').before('<tr><td>New Insert</td></tr>');
    return false;
});
13
ответ дан 8 December 2019 в 18:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: