jquery 在调用 jquery 的行后添加表行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2704100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:06:19  来源:igfitidea点击:

Jquery add table row after the row which calling the jquery

jqueryhtml

提问by Répás

I've got a table.

我有一个table.

<table id="servers" ...>
...
{section name=i loop=$ownsites}
<tr id="site_id_{$ownsites[i].id}">
...
<td>{$ownsites[i].phone}</td>
<td class="icon"><a id="{$ownsites[i].id}" onClick="return makedeleterow(this.getAttribute('id'));" ...></a></td>
</tr>       
{/section}
<tbody>
</table>

And this JavaScript.

还有这个 JavaScript。

<script type="text/javascript">
function makedeleterow(id)
    {
        $('#delete').remove();
        $('#servers').append($(document.createElement("tr")).attr({id: "delete"}));
        $('#delete').append($(document.createElement("td")).attr({colspan: "9", id: "deleter"}));
        $('#deleter').text('Biztosan t?r?lni szeretnéd ezt a weblapod?');
        $('#deleter').append($(document.createElement("input")).attr({type: "submit", id: id, onClick: "return truedeleterow(this.getAttribute('id'))"}));
        $('#deleter').append($(document.createElement("input")).attr({type: "hidden", name: "website_del", value: id}));
    }
</script>

It's working fine, it makes a trafter the table's last trand puts the info to it, and the delete function also works fine.

它工作正常,它trtable's 最后一个之后添加tr信息,并且删除功能也可以正常工作。

But I'd like to make this append AFTERthe tr(with tdclass="icon") which is calling the script. How can I do this?

但是我想在调用脚本的(with )之后进行此附加。我怎样才能做到这一点?trtdclass="icon"

回答by Geoff

You can use the .after() function in jQuery to append some content after another element.

您可以使用 jQuery 中的 .after() 函数在另一个元素之后附加一些内容。

So instead of

所以代替

$("servers").append( ... );

you would use

你会用

$("#" + id + ).closest( "tr" ).after( ... );

or you could also use

或者你也可以使用

$( ... ).insertAfter( $("#" + id ).closest( "tr" ) );

which is essentially equivalent.

这本质上是等效的。

See http://api.jquery.com/after/for full details.

有关完整详细信息,请参阅http://api.jquery.com/after/

回答by Dontlose

try .prepend()instead of .append()

尝试.prepend()代替.append()

回答by asalam345

interst tr id then onclick="xyz()"

interst tr id 然后 onclick="xyz()"

something like this:

像这样:

<tr id=""  onclick="xyz()"><td></td><td></td></tr>
<script>
    function xyz()
    {
    var newrow = "<tr><td>data1</td><td>data1</td></tr>";        
    $("#tr_id").after(newrow);
    }
</script>