Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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/1/dart/3.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 比较字符串和更改行的颜色_Javascript_Arrays_String_Compare_Html Table - Fatal编程技术网

Javascript 比较字符串和更改行的颜色

Javascript 比较字符串和更改行的颜色,javascript,arrays,string,compare,html-table,Javascript,Arrays,String,Compare,Html Table,我试图创建一个方法来比较不同数组中两个元素的字符串。所有字符串都在表上的行中。如果这两个字符串相等,则该行应为绿色。否则,该行应为红色。我已经完成了表和数组的工作,我只需要帮助比较和更改特定行的颜色 <style> .tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:soli

我试图创建一个方法来比较不同数组中两个元素的字符串。所有字符串都在表上的行中。如果这两个字符串相等,则该行应为绿色。否则,该行应为红色。我已经完成了表和数组的工作,我只需要帮助比较和更改特定行的颜色

<style>
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-

width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-

style:solid;border-width:1px;overflow:hidden;word-break:normal;}
</style>

<table class="tg" style="undefined;table-layout: fixed; width: 363px">
<colgroup>
<col style="width: 121px">
<col style="width: 121px">
<col style="width: 121px">
</colgroup>
<tr>
<th class="tg-031e">Question:</th>
<th class="tg-031e">Correct Answer:</th>
<th class="tg-031e">Your Answer:</th>
  </tr>
  <tr>

<td class="tg-031e">1.</td>
<td class="tg-031e"></td>
<td class="tg-031e"></td>
  </tr>
  <tr>

<td class="tg-031e">2.</td>
<td class="tg-031e"></td>
<td class="tg-031e"></td>
  </tr>
  <tr>
<td class="tg-031e">3.</td>
<td class="tg-031e"></td>
<td class="tg-031e"></td>
  </tr>
  <tr>
<script>
    var a1 = new Array()
        a1[0] = new Array("the");
        a1[1] = new Array("of");
        a1[2] = new Array("and");
    var a2 = new Array()
        a2[0] = new Array("the");
        a2[1] = new Array("ha");
        a2[2] = new Array("and");


var nodes = document.querySelectorAll('td.tg-031e:nth-child(2)');
    [].forEach.call(nodes, function(node, index) {
    node.innerText = a1[index][0];
    });

    var nodes = document.querySelectorAll('td.tg-031e:nth-child(3)');
        [].forEach.call(nodes, function(node, index) {
        node.innerText = a2[index][0];
        });

</script>

.tg{边框折叠:折叠;边框间距:0;}
.tg td{字体系列:Arial,无衬线;字体大小:14px;填充:10px 5px;边框样式:实心;边框-
宽度:1px;溢出:隐藏;分词:正常;}
.tg th{字体系列:Arial,无衬线;字体大小:14px;字体重量:正常;填充:10px 5px;边框-
样式:实体;边框宽度:1px;溢出:隐藏;分词:正常;}
问题:
正确答案:
你的回答是:
1.
2.
3.
var a1=新数组()
a1[0]=新数组(“新数组”);
a1[1]=新数组(“of”);
a1[2]=新阵列(“和”);
var a2=新数组()
a2[0]=新阵列(“新阵列”);
a2[1]=新阵列(“ha”);
a2[2]=新阵列(“和”);
var节点=document.querySelectorAll('td.tg-031e:n第n个子节点(2)');
[]forEach.call(节点、函数(节点、索引){
node.innerText=a1[索引][0];
});
var nodes=document.queryselectoral('td.tg-031e:n第n个子项(3)');
[]forEach.call(节点、函数(节点、索引){
node.innerText=a2[索引][0];
});

您可以对每行使用一个循环,并比较数组值,如果它们相同,则分配一个类

var a1=新数组()
a1[0]=新数组(“新数组”);
a1[1]=新数组(“of”);
a1[2]=新阵列(“和”);
var a2=新数组()
a2[0]=新阵列(“新阵列”);
a2[1]=新阵列(“ha”);
a2[2]=新阵列(“和”);
var trs=[].slice.call(document.queryselectoral('table.tg tr'),1);
trs.forEach(功能(tr,idx){
tr.children[1].innerText=a1[idx][0];
tr.children[2].innerText=a2[idx][0];
if(a1[idx][0]==a2[idx][0]){
tr.className='相同';
}
})
.tg{
边界塌陷:塌陷;
边界间距:0;
}
.tg td{
字体系列:Arial,无衬线;
字体大小:14px;
填充:10px 5px;
边框样式:实心;
边框宽度:1px;
溢出:隐藏;
断字:正常;
}
.tg th{
字体系列:Arial,无衬线;
字体大小:14px;
字体大小:正常;
填充:10px 5px;
边框样式:实心;
边框宽度:1px;
溢出:隐藏;
断字:正常;
}
同样{
背景颜色:绿色;
}

问题:
正确答案:
你的回答是:
1.
2.
3.

非常感谢!只是一个简单的问题,如果我想添加更多的行,我会怎么做?