Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery将新行追加到表-检测现有行,但不添加新行_Javascript_Jquery - Fatal编程技术网

Javascript jQuery将新行追加到表-检测现有行,但不添加新行

Javascript jQuery将新行追加到表-检测现有行,但不添加新行,javascript,jquery,Javascript,Jquery,我有一个基本的HTMl表格如下 jQuery(文档).ready(函数(){ /*计算总数*/ jQuery('table tr.customRow')。每个(函数(i,行){ log('Found Row'); jQuery(this.append('newtestrow'); }); }); 我的第一排 我的第二排 我的第三排 您没有试图在现有的trs之前添加新的trs;你正在尝试将它们插入其中 而不是: jQuery(this).append('<tr>New Test R

我有一个基本的HTMl表格如下

jQuery(文档).ready(函数(){
/*计算总数*/
jQuery('table tr.customRow')。每个(函数(i,行){
log('Found Row');
jQuery(this.append('newtestrow');
});
});

我的第一排
我的第二排
我的第三排

您没有试图在现有的
tr
s之前添加新的
tr
s;你正在尝试将它们插入其中

而不是:

jQuery(this).append('<tr>New Test Row</tr>');
jQuery(this.append('newtestrow');
你想要:

jQuery(this).before('<tr>New Test Row</tr>');
jQuery(this).before('newtestrow');

此外,正如@Rory McCrossan所指出的,您不能在
tr
节点中直接使用文本节点-您需要一个
td
容器。

您的代码中有两个问题。首先,您尝试添加的HTML无效<代码>
元素不能包含文本节点。您需要将内容包装在
td
th

其次,
append()
将把新的
tr
放在现有的
tr
中,这同样是无效的HTML。根据您的目标,请使用before()

还要注意,因为新内容在所有情况下都是相同的,所以这里不需要
each()
循环。jQuery将隐式地在集合上循环,并为您在每个选定元素上创建内容。试试这个:

jQuery(函数($){
$('table tr.customRow')。在('New Test Row')之前;
});

我的第一排
我的第二排
我的第三排