【发布时间】:2019-03-11 20:54:27
【问题描述】:
我在 JavaScript 上有一个从 firebase 表中获取数据的函数。获取的数据将附加到我的 HTML 中的 <tbody>。当按钮(作为弹出模式开启器)正常使用 HTML 生成时,按钮按预期工作(即使创建了它的多个实例),但是当它使用脚本生成时,它无法工作,即使如果使用单个按钮。
modal.js:
var modals = document.getElementsByClassName('modal');
// Get the button that opens the modal
var btns = document.getElementsByClassName("openmodal");
var spans=document.getElementsByClassName("close");
for(let i=0;i<btns.length;i++){
btns[i].onclick = function() {
modals[i].style.display = "block";
}
}
for(let i=0;i<spans.length;i++){
spans[i].onclick = function() {
modals[i].style.display = "none";
}
}
reportsTable.js(用于获取数据,并将其附加到表中)
function reportsTable() {
var rootRef = firebase.database().ref().child("reports");
rootRef.on("value", snap => {
$("#table_body").html("");
snap.forEach(snap => {
var Category = snap.child("Category").val();
var Dates = snap.child("Date").val();
var Location = snap.child("Location").val();
var Report = snap.child("Report").val();
var Status = snap.child("Status").val();
$("#table_body").append("<tr><td>" + Category + "</td><td>" + Dates + "</td><td>" + Location + "</td><td>" + Report + "</td><td>" + Status + "</td><td>" +
"<button class='openmodal myBtn'>View</button>" +
"<div class='modal myModal'>" +
"<div class='modal-content'>" +
"<span class='close'>×</span>" +
"<p>Report Category: " + Category + " </p>" +
"<p>Report Date: " + Dates + " </p>" +
"<p>Location: " + Location + " </p>" +
"<p>Report Details: " + Report + " </p>" +
"<p>Report Status: " + Status + " </p>" +
"</div>" +
"</div></td></tr>")
})
});
}
HTML:
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Category</th>
<th>Date</th>
<th>Location</th>
<th>Report</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Category</th>
<th>Date</th>
<th>Location</th>
<th>Report</th>
<th>Status</th>
<th>Action</th>
</tr>
</tfoot>
<tbody id="table_body">
<tr>
<td>
<button class="openmodal myBtn">Open Modal</button>
<div class="modal myModal">
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
我尝试在我的按钮中添加onclick 函数,但它仍然不会触发弹出窗口。我的代码中是否缺少任何内容?
【问题讨论】:
标签: javascript html firebase firebase-realtime-database