javascript - 切换隐藏/显示不适用于子 div
问题描述
我有一个脚本,它从 Google 表格中获取数据并将其显示为网页 - 使用 JS 和 Tabletop.js。
工作表中有多个条目,因此网页中有多个条目。为了组织数据,我有一个隐藏/显示按钮。当在第一个条目上单击该按钮时,它会起作用。但是,当单击任何其他按钮时,它会隐藏或显示第一个条目数据,而不是它自己的!
如何隐藏/显示每个单独的条目数据?下面是我正在使用的代码!
我是 JavaScript 新手 - 在此先感谢!
PS - 我很难写出问题的标题!
<link href="../common/cats-copy.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<style>
#add-info {
display: none
}
</style>
<body>
<div class="container">
<h1>Resturants</h1>
<div id="content"></div>
<script id="cat-template" type="text/x-handlebars-template">
<div class="entry">
<h5>{{establishment_name}}</h5>
<h6>Area: {{area}}</h6>
<h6>Cuisine: {{cuisine}}</h6>
<button id="btn" class="button-primary" onclick="myFunction()">Hide</button>
<div id="add-info">
<h6>Address: {{address}}</h6>
<h6>Google Maps: {{google_maps_location}}</h6>
<h6>Opening Times: {{opening_times}}</h6>
<h6>Rating: {{rating}}</h6>
<h6>Added By: {{added_by}}</h6>
<h6>Date Added: {{date_added}}</h6>
</div>
</div>
</script>
</div>
<!-- Don't need jQuery for Tabletop, but using it for this example -->
<script type="text/javascript" src="handlebars.js"></script>
<script type="text/javascript" src="../../src/tabletop.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var public_spreadsheet_url = 'https://docs.google.com/spreadsheets/d/1h5zYzEcBIA5zUDc9j4BTs8AcJj-21-ykzq6238CnkWc/edit?usp=sharing';
$(document).ready( function() {
Tabletop.init( { key: public_spreadsheet_url,
callback: showInfo,
parseNumbers: true } );
});
function showInfo(data, tabletop) {
var source = $("#cat-template").html();
var template = Handlebars.compile(source);
$.each( tabletop.sheets("food").all(), function(i, food) {
var html = template(food);
$("#content").append(html);
});
}
</script>
<script>
function myFunction() {
var x = document.getElementById("add-info");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
</body>
</html>
解决方案
您页面上的所有条目是否都是从给定的模板填充的,这意味着它们div
与类有关entry
吗?如果是这样,我认为您的问题如下:您entry
div
的孩子div
患有id="add-info"
. 当你点击按钮时,你的myFunction()
处理函数(但是元素的属性在整个文档中必须是唯一的。参见或的描述。div
document.getElementById("add-info");
div
id="add-info"
id
id
getElementById()
因此,问题的根本原因是id
文档中不应该多次使用相同的内容。您会得到您所看到的行为,因为getElementById()
恰好返回对它在页面上找到的第一个元素的引用,而不管您单击哪个按钮。但我相信你当时处于未定义的行为领域。
解决该问题的一种方法是以某种方式提供myFunction()
有关单击了哪个按钮的信息,同时使div
您想要操作的每个按钮都独一无二,以便更容易找到它们。例如,您可以将页面上餐厅的顺序用作其“索引”,并将其用作您想要隐藏/显示id
的顺序。div
当您调用点击处理程序时,您还可以将此索引作为参数传递:
...
<button id="btn" class="button-primary" onclick="myFunction('{{index}}')">Hide</button>
<div id="{{index}}">
<!-- The rest of the code here... -->
...
...将索引添加到您的模板上下文中,因此Handlebars
可以填写{{index}}
占位符:
...
$.each( tabletop.sheets("food").all(), function(i, food) {
food.index = i // Give your context its 'index'
var html = template(food);
$("#content").append(html);
});
...
...然后稍微改变你的函数以使用给定的参数而不是总是寻找div
with id="add-info"
:
function myFunction(indexToToggle) {
var x = document.getElementById(indexToToggle);
// rest of the code is same
使用这种方法,我希望您的 DOM 以 s 结束,其中div
sid
只是数字("3"
,"4"
等),并且您的点击处理程序也应该使用这些作为参数来调用。
另请注意,您的<button>
元素具有id="btn"
. 如果您在页面上重复该模板,您将有多个<button>
具有相同id
. 如果您开始尝试button
通过 s获取对您的引用,id
那么您也会遇到类似的问题,因为id
s 不会是唯一的。
推荐阅读
- android - 单元测试 Android Studio Textview
- spring-boot - 简单的 Http 请求记录到数据库
- postgresql - 使用 psql 调用 postgres 数据库时,如何自动运行初始化脚本?
- python - 使用 TensorFlow 2.1 加载自定义模型
- reactjs - 在反应钩子中,我如何清空我的状态并同时显示空输入?
- excel - 使用 Excel VBA 损坏文件删除命名范围
- python - MyProgrammingLab 三角数问题
- python - 每行最多创建 8 个项目(python)
- c# - (C#.NET-WPF) 将所有注意力集中在应用程序窗口上
- java - Maven / Spring Boot 项目 - 如何跳过 @SpringBootTest