如果链接没有 href,则隐藏父 TR
Hide parent TR if link has no href?
我目前有这个:
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="http://www.google.com">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="">Visit Website</a>
</td>
</tr>
</tbody>
</table>
我需要一些 JavaScript 来:
如果<a>
没有 href (href=") 则隐藏包装它的 TR。
如何使用javascript实现这一点?
您可以使用
仅供参考:
属性等于选择器来获取带有href=""
a
标签,然后通过closest()
获取tr
$('a[href=""]').closest('tr').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="http://www.google.com">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="">Visit Website</a>
</td>
</tr>
</tbody>
</table>
或者,您可以使用:has()
或has()
属性等于选择器。
$('tr:has(a[href=""])').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="http://www.google.com">Visit Website</a>
</td>
</tr>
<tr>
<td>
<a href="">Visit Website</a>
</td>
</tr>
</tbody>
</table>
仅供参考:
如果要选择没有href
属性a
标签,则可以使用:not()
选择器,例如:
$('a[href=""],a:not(a[href])').closest('tr').hide();
https://jsfiddle.net/看看
这个现在它的工作,请检查
如果你只想检查 href="
只需尝试
$('a[href=""]').parent().parent().hide();
或
$('a[href=""]').closest("tr").hide();
如果您还想仅检查 href=" 或未给出 href 属性
只需尝试
$('a[href=""],a:not([href])').parent().parent().hide();
或
$('a[href=""],a:not([href])').closest("tr").hide();
相关文章:
- 使用Javascript,如何显示<tr>在另一<tr>悬停时,隐藏第二个<tr>
- jQuery只隐藏<tr>在一张桌子上
- 获取TR之外最接近的隐藏值
- 防止隐藏的 tr 破坏 td 宽度
- 根据TD类别隐藏TR
- Javascript没有't隐藏tr元素
- 隐藏<tr>包含id中的变量's
- 将表的隐藏状态更改为可见状态>点击TR
- 隐藏具有th和colspan的tr
- 如果链接没有 href,则隐藏父 TR
- 隐藏父头,如果 tbody 中的所有 tr 都隐藏显示:无
- 在特定时间后从表中隐藏 tr
- 隐藏/显示基于所选单选按钮的表 TR - jquery
- 使用 javascript 隐藏具有相同类的所有 tr
- 选择任何 TR 和/或 TD 并隐藏元素
- 隐藏tr元素,以防在其中单击
- 隐藏tr.中的整行
- 仅当td在特定html标记后不包含任何内容时,才隐藏tr
- 使用jQuery validate可以显示和隐藏TR ID元素吗
- 基于多个CSS类和来自多个复选框的输入显示/隐藏tr元素