jquery - 如何在两个不同的表中使用 JQuery 更改 TH 的样式
问题描述
我有这个带有两个不同表格的 HTML:
<table class="mTabla" id="sdsdsdsf" style="margin: 0px auto; width: 100%; text-align: left; border-collapse: collapse;" border="1" rules="all" cellspacing="0">
<tbody>
<tr>
<th style="display: none;" scope="col">#IdOculto</th>
<th style="width: 200px;" scope="col">A1</th>
<th style="width: 250px;" scope="col">A2</th>
<th style="width: 200px;" scope="col">A3</th>
</tr>
<tr id="sdsdsds" style="cursor: pointer;">
<td style="display: none;">57</td>
<td>D1</td>
<td>D1</td>
<td>D1</td>
</tr>
<tr id="sdsdsds" style="cursor: pointer;">
<td style="display: none;">58</td>
<td>D2</td>
<td>D2</td>
<td>D2</td>
</tr>
</tbody>
</table>
<table class="mTabla" id="sdsdsd" style="margin: 0px auto; width: 100%; text-align: left; border-collapse: collapse;" border="1" rules="all" cellspacing="0">
<tbody>
<tr>
<th style="display: none;" scope="col">#IdOculto</th>
<th style="width: 200px;" scope="col">A1</th>
<th style="width: 250px;" scope="col">A2</th>
<th style="width: 200px;" scope="col">A3</th>
</tr>
<tr id="sdsdsds" style="cursor: pointer;">
<td style="display: none;">57</td>
<td>D1</td>
<td>D1</td>
<td>D1</td>
</tr>
<tr id="sdsdsds" style="cursor: pointer;">
<td style="display: none;">58</td>
<td>D2</td>
<td>D2</td>
<td>D2</td>
</tr>
</tbody>
</table>
我需要使每个表的第一个可见 TH 元素具有边框半径。需要考虑的事情:我不能编辑 HTML 或 CSS 文件,我被要求只使用 JQuery 来做,我迷失了。表的 ID 不是代码中的 ID,但无论如何它们是动态的。
我的 jQuery 代码:
var testimonialElements = $(".mTabla");
for(var i = 0; i < testimonialElements.length; i++){
$('th:visible:eq(0)').css("border-radius", "6px 0 0 6px");
}
这段代码部分满足了我的需要。它将我想要的样式应用于第一个可见的 TH mTable
,但不会将其应用于第二个的第一个可见 TH mTable
。我需要一个对两个表都有效的解决方案。我会很感激你的帮助。
解决方案
这是将边框半径应用于两个表的第一个可见 TH 的方法。
$(".mTabla").each(function(){
$(this).find('th:visible:eq(0)').css("border-radius","6px 0 0 6px");
})
I don't know if it is a good idea to add a border-radius to a TH element, but that isn't the question. :)
推荐阅读
- git - 当我使用 git checkout -b 或 git rebase 时,历史记录有什么不同吗
- javascript - 挂钩值不会立即更新。异步常量
- c# - 使用队列的线程可重用性(线程之间的双向通信)
- sql - SQL ORACLE的日期输入
- node.js - 是否有带有原生/生产级 HTTP API 的消息队列?
- angular - 如何在 Angular 中将数据从父组件传递到子组件?
- javascript - 如何在javascript中通过1个索引将数组foreach数组数据
- flutter - 使用 GetX 通过参数发送数据
- python-3.6 - 覆盖继承的默认属性方法
- video - 如何使用 exiftool 生成 track.log 文件?