Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.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
使用jQuery脚本,根据第一个单元格中的文本将单元格添加到表中_Jquery - Fatal编程技术网

使用jQuery脚本,根据第一个单元格中的文本将单元格添加到表中

使用jQuery脚本,根据第一个单元格中的文本将单元格添加到表中,jquery,Jquery,我有这张桌子 <table width="100%" border="0" cellspacing="0" cellpadding="0" class="test-class"> <tr> <td rowspan="6">TEST123<script type="text/javascript">if(window.VCompare){VCompare('TEST123', 4);}</script></td>

我有这张桌子

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="test-class"> 
 <tr> 
  <td rowspan="6">TEST123<script type="text/javascript">if(window.VCompare){VCompare('TEST123', 4);}</script></td>
  <td rowspan="6"><img src="/images/clear1x1.gif" width="5" height="5"></td> 
  <td rowspan="6" background="/images/test.gif"><img src="/images/clear1x1.gif" width="1" height="1"></td>
  <td rowspan="4"><img src="/images/clear1x1.gif" width="5" height="5"></td>
  <td colspan="9"><img src="/images/clear1x1.gif" width="5" height="5"></td>
 </tr> 
</table>

TEST123if(window.VCompare){VCompare('TEST123',4);}
我需要在没有脚本标记的情况下获取第一个单元格的值,然后在获取文本的第一个单元格之前添加一个单元格。我试过像

var matchtext = "TEST123";
jQuery("tr").each(function() {
 var data = jQuery(this).find("td:eq(0)").text();
 var data = jQuery.trim(data);
 if (data == matchtext){
    jQuery(this).before("<td class='testclass' rowspan='6'>test inserted!</td>");
 }
});
var matchtext=“TEST123”;
jQuery(“tr”)。每个(函数(){
var data=jQuery(this).find(“td:eq(0)”).text();
var data=jQuery.trim(数据);
如果(数据==匹配文本){
jQuery(this).before(“插入测试!”);
}
});
并在表格中循环,但该脚本使我感到厌烦。

尝试更改行:

if (data == matchtext){


如果将
data
记录到firebug控制台,您会看到字符串是“TEST123if(window.VCompare){VCompare('TEST123',4);}”,而不仅仅是“TEST123”,因为javascript部分也算作文本

将该JS部件移出表,请参见:


测试123
if(window.VCompare){VCompare('TEST123',4);}
此外,您的JS代码并没有完全按照您的预期执行,工作代码:

var matchtext = "TEST123";
$("tr").each(function() {
 var $this = $(this),
     data = $.trim($this.children(':first-child').text());

 if (data == matchtext){
     $this.prepend("<td class='testclass' rowspan='6'>test inserted!</td>");
 }
});
var matchtext=“TEST123”;
$(“tr”)。每个(函数(){
变量$this=$(this),
data=$.trim($this.children(':first child').text());
如果(数据==匹配文本){
$this.prepend(“插入测试!”);
}
});
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="test-class"> 
 <tr> 
  <td rowspan="6">TEST123</td>
  <td rowspan="6"><img src="/images/clear1x1.gif" width="5" height="5"></td> 
  <td rowspan="6" background="/images/test.gif"><img src="/images/clear1x1.gif" width="1" height="1"></td>
  <td rowspan="4"><img src="/images/clear1x1.gif" width="5" height="5"></td>
  <td colspan="9"><img src="/images/clear1x1.gif" width="5" height="5"></td>
 </tr> 
</table>
<script type="text/javascript">if(window.VCompare){VCompare('TEST123', 4);}</script>
var matchtext = "TEST123";
$("tr").each(function() {
 var $this = $(this),
     data = $.trim($this.children(':first-child').text());

 if (data == matchtext){
     $this.prepend("<td class='testclass' rowspan='6'>test inserted!</td>");
 }
});