javascript - 您如何使用 JavaScript 从内部表的 TD 中影响 DIV?
问题描述
我有一个包含我要阻止的内容的外部 DIV。然而,只有内部 TD 具有作为限定符的属性。
我已经得到了代码(来自一个 SO 用户)并使用 TamperMonkey 来实现它,它就像一个魅力。但是它删除的太少并保留了父 DIV。我对 JavaScript 知之甚少,无法影响外部 DIV。
<DIV>
<Table></TABLE>
<Table>
<td attribute="desiredTarget"></td>
</TABLE>
<Table></TABLE>
</DIV>
预期:DIV 不应基于 TD 内容显示结果:DIV 仍显示
解决方案
使用jQuery会比 PURE JS 更好
$('td[attribute="X"]')
.parent() //TO TR
.parent() //TO TBODY
.parent() //TO TABLE
.parent() //TO DIV
.css("background-color", "red"); ///Your command
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
1
<table border="1">
<tr>
<td>Unnesscessary</td>
</tr>
</table>
2
<table border="1">
<tr>
<td attribute="X">xxx</td>
<td>Unnesscessary</td>
</tr>
<tr>
<td>Unnesscessary</td>
</tr>
</table>
3
<table border="1">
<tr>
<td>Unnesscessary</td>
</tr>
<tr>
<td>Unnesscessary</td>
</tr>
</table>
</div>
推荐阅读
- azure - ADLS Gen 1 与 ADLA U-SQL 作业的通用存储 Blob 相比性能较差
- angular - 调整mat-form-field Angular Material的高度
- jpa - 在 JPA 中,如何读取 Sybase 存储过程的返回值?
- windows - 为什么 _gmtime32 和 _gmtime64 支持的最高日期在不同年份的 1 月 19 日?
- python - 如何在 numpy 中加速大张量的 logsumexp?
- c# - 使用带有 for 循环的 Raycast 制作视野(使用 Unity C#)
- python - 如果其他列的值与子字符串匹配而不仅仅是相等比较,我如何提取熊猫 df 单元格值
- ajax - ajax 没有得到 cakephp 代码的任何响应
- ios - 我也可以在 xcassets 之类的源代码中切片图像资源吗?
- python - Selenium Explicit 等待在 Python 中无法正常工作