[英]Jquery - Change table cell values based on table row class
I have a table displays opening hours for various locations and looks somewhat like the following structure; 我有一个表格,显示各个地点的营业时间,看起来有点像以下结构;
<table class="tblServices">
<tbody>
<tr class="name loc1">
<td class="name">loc1</td>
<td class=""tcDayTimes">9-5</td>
<td class="tcDayTimes">9-5</td>
</tr>
<tr class="name loc2">
<td class="name">loc1</td>
<td class="tcDayTimes">9-5</td>
<td class="tcDayTimes">9-5</td>
</tr>
</tbody>
</table>
What I would like to be able to do is change the value of all table cells with the class "tcDayTimes" within the row the class "loc2", I have gotten close with the following jquery but its replacing the values for every cell with that class no matter what table row its in. 我想做的是在类“ loc2”的行中用类“ tcDayTimes”更改所有表单元格的值,我已经与下面的jQuery保持了联系,但是用它替换了每个单元格的值类,无论在哪个表行中。
<script type="text/javascript">
$('.tblServices > tbody > tr.loc2').each(
$('td.tcDayTimes').html("24 hours")
Can some provide some advice as to where I am going wrong? 有人可以提供一些有关我要去哪里错误的建议吗?
Thanks in advance 提前致谢
You have a typo in your html, but otherwise... 您的html中有错字,但否则...
$('.tblServices > tbody > tr.loc2').each(function(){
$('td.tcDayTimes', this).html("24 hours")
});
Where this
is the tr you are iterating over. this
是您要遍历的tr。 The second argument to the $()
is the context. $()
的第二个参数是上下文。
OR 要么
$('.tblServices > tbody > tr.loc2 td.tcDayTimes').html("24 hours");
You were close. 你近了 In your selector, you need to add > .tcDayTimes
, which stores the items into a NodeList . 在选择器中,您需要添加> .tcDayTimes
,它将项目存储到NodeList中 。 Then use .html()
to change the html of all those elements. 然后使用.html()
更改所有这些元素的html。
$(".tblServices > tbody > .loc2 > .tcDayTimes").html("24 hours");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="tblServices"> <tbody> <tr class="name loc1"> <td class="name">loc1</td> <td class="tcDayTimes">9-5</td> <td class="tcDayTimes">9-5</td> </tr> <tr class="name loc2"> <td class="name ">loc1</td> <td class="tcDayTimes">9-5</td> <td class="tcDayTimes">9-5</td> </tr> </tbody> </table>
You can also use the following method to do this in vanilla javascript. 您也可以在香草javascript中使用以下方法执行此操作。 This basically uses .querySelectorAll()
to select the values you wanted and store them into a NodeList (like an array). 这基本上使用.querySelectorAll()
来选择所需的值,并将它们存储到NodeList (如数组)中。 Then you can use .innerHTML
to set the html of those elements. 然后,您可以使用.innerHTML
设置这些元素的html。
var elements = document.querySelectorAll(".tblServices > tbody > .loc2 > .tcDayTimes"); elements.forEach(e => { e.innerHTML = "24 hours"; });
<table class="tblServices"> <tbody> <tr class="name loc1"> <td class="name">loc1</td> <td class="tcDayTimes">9-5</td> <td class="tcDayTimes">9-5</td> </tr> <tr class="name loc2"> <td class="name ">loc1</td> <td class="tcDayTimes">9-5</td> <td class="tcDayTimes">9-5</td> </tr> </tbody> </table>
You dont need the >
. 您不需要>
。 You can just have .tblServices tbody .loc2 .tcDayTimes
, but it makes it look neater, and I would keep it there if I were you. 您可以只具有.tblServices tbody .loc2 .tcDayTimes
,但是它看起来更加整洁,如果您是我,我会保留在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.