微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

之前的Jquery Insert元素

我试图插入一个块元素在另一个之前插入一些东西.我不确定我是否使用正确的方法,但这是我的代码.希望你们能帮忙.谢谢!

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>

解决方法

< TD>应始终在< tr>中.

我可能会让你的功能像这样:

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

原文地址:https://www.jb51.cc/jquery/176853.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐