Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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,我需要使用列id更改一组列的背景颜色。前两列的背景不需要更改。如何在Jquery中实现? 这是我到目前为止已经尝试过的。我已经在这里添加了我的完整脚本,你可以得到更多关于它的信息 var fromtime = obj[i].FromTime; // Here fromtime gives id of a column not index var totime = obj[i].totime; // Here totime gives id of a column not index $(

我需要使用列id更改一组列的背景颜色。前两列的背景不需要更改。如何在Jquery中实现? 这是我到目前为止已经尝试过的。我已经在这里添加了我的完整脚本,你可以得到更多关于它的信息

 var fromtime = obj[i].FromTime; // Here fromtime gives id of a column not index
  var totime = obj[i].totime; // Here totime gives id of a column not index
 $(row1).children('td').slice(fromtime,totime).not('td:eq(0)').not('td:eq(0)').css({ "background-color": workcolor }); 
编辑: 我已经添加了我的HTML代码

<table class="table table-striped" id="table1">
                <thead>
                    <tr>
                        <th class="" id="profilepic">Image</th>
                        <th id="empName">Employee</th>
                        <th id="00">00:00</th>
                        <th id="01">01:00</th>
                        <th id="02">02:00</th>
                        <th id="03">03:00</th>
                        <th id="04">04:00</th>
                        <th id="05">05:00</th>
                        <th id="06">06:00</th>
                        <th id="07">07:00</th>
                        <th id="08">08:00</th>
                        <th id="09">09:00</th>
                        <th id="10">10:00</th>
                        <th id="11">11:00</th>
                        <th id="12">12:00</th>
                        <th id="13">13:00</th>
                        <th id="14">14:00</th>
                        <th id="15">15:00</th>
                        <th id="16">16:00</th>
                        <th id="17">17:00</th>
                        <th id="18">18:00</th>
                        <th id="19">19:00</th>
                        <th id="20">20:00</th>
                        <th id="21">21:00</th>
                        <th id="22">22:00</th>
                        <th id="23">23:00</th>
                    </tr>
                </thead>
                <tbody id="body1">

                </tbody>

每次我都会添加一个名为“row1”的新行,该行的单元格背景色需要根据
“fromtime”
“ToTime”
值进行修改。有
子项('td')
td used意味着整行都被涂上了颜色。但我只需要特定的单元格。

您的代码有许多其他不适用或似乎与问题无关的内容,然后,我在这里制作一个代码,可以帮助您准确地回答问题(将
背景应用于列,而不是第一列和第二列)。


我使用了一个非常基本的逻辑,从索引2开始循环遍历所有列(因为您不希望前两列有BG)

也许你不会像我在下面所做的那样使用它,但我相信这会有所帮助,你可能会找到一种在代码中使用它的方法。 请看一下片段,如果您需要更多帮助,请告诉我

编辑
如果您只需要一些列出的ID来接收
背景
,也许您可以拥有或接收一个数组,然后检查它们是否是您所需要的。这也将改变foor循环,因为现在不需要从2开始,您可以循环所有列,并将背景应用于数组中列出的列,如下所示:

选项1
循环遍历所有列(以确保只找到列,而不找到任何其他元素),然后检查ID。
$(文档).ready(函数(){
变量arrayOfIDs=[“02”、“03”、“04”、“07”、“10”、“15”、“21”];
var myTable=$(“.table striped”);
var myColumns=$(myTable.find(“th”);
//循环遍历所有列
myColumns.each(函数(){
var列=此;
//循环查看所有要更改的ID
对于(变量i=0;i
th{
边框:1px纯黑;
}

形象
雇员
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00

Html也请?请显示完整的脚本和Html。为什么不简单地使用
css
?不需要jQuery/javascript,因为需要应用
background
的列是动态的,他不知道哪一个需要
你的代码运行得很好。但是我需要使用列的“ID”进行迭代,比如需要从ID为17到22的列中为背景着色,这意味着我应该怎么做?你如何知道需要使用哪些ID进行迭代?它将在一个数组中,例如:
var idsIterate=[“17”、“18”、“19”…]
?我将为已经可用的列添加一行,例如从17到22表示如何更改该列的背景色?对不起,不明白,如何添加行?行是一个元素吗?我的意思是,我正在将一个新行附加到表“table striped”中,该行中有某些单元格背景需要更改。某种意义上的单元格,一个列ID的数组。怎么做?
 $(row1).children('td').not('td:eq(0)').not('td:eq(0)').css({ "background-color": workcolor });