jquery - 如何使用 jQuery 脚本定位特定的自定义 html 标签
问题描述
我正在尝试使用 jQuery 隐藏某些内容并在表格中显示表单,但现在我隐藏或显示自定义标签上的所有内容。我怎样才能让这个脚本只针对我可以给这些标签的 ID?
$(document).ready(function() {
$(".hide").click(function() {
$("p").hide(100);
$("z").show(100);
});
$(".show").click(function() {
$("p").show(100);
$("z").hide(100);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table table-bordered">
<th>début vacance</th>
<th>fin vacance</th>
<th>supprimer vacances</th>
<tr th:each="vacations : ${selectedUser.getVacations()}">
<td th:utext="${vacations.getStartVacation()} ">...</td>
<td th:utext="${vacations.getEndVacation()} + ${vacations.id}">...</td>
<td>
<p th:text="${vacation.fistname}">...</p>
<z th:text="${vacations.id}">...</z>
<button class="hide">Hide</button>
<button class="show">Show</button>
</td>
</table>
解决方案
像这样试试。这将检查最近的父 tr 并在其中找到 p 或 z。如果您也为 p 添加了多个容器,这在任何情况下都不会失败
<script>
$(document).ready(function(){
$(".hide").click(function(){
$(this).parents('tr').find("p").hide(100);
$(this).parents('tr').find("z").show(100);
});
$(".show").click(function(){
$(this).parents('tr').find("p").show(100);
$(this).parents('tr').find("z").hide(100);
});
});
</script>
希望这可以帮助
推荐阅读
- android - 如何在 MediaStore.Downloads 集合中保存视频缩略图。安卓Q
- javascript - 单张(7 轨)专辑的简单音乐播放器(JavaScript 错误)
- ruby-on-rails - 在solidus 上读取的第一个文件是什么?
- ruby-on-rails - NoMethodError:Capybara 测试期间未定义的方法“last_comment”
- android - Android Studio 给出关于 JDK 位置的警告
- javascript - 如何使用 Google 登录检测用户是否在页面加载时登录
- python - Byzantine Consensus Randomized - Monte Carlo Implementation with matrix for value sending
- android - 如何在android中使用Firebase MLKit进行文本检测时制作平滑的覆盖界面
- ios - 从类类型中获取类对象
- javascript - ExpressJs 等待任务或超时