【发布时间】:2016-10-07 14:47:30
【问题描述】:
我在显示弹出窗口时遇到问题。我有 2 个弹出窗口。它们是通过 php foreach 生成的(在 echo 中)...问题是,每次只会在页面上显示 FIRST 跨度,但 SECOND 不起作用。 PHP代码在这里:
echo __( "<div class='action' style='margin-bottom: -20px'>
<div id='box' style='background-color:". $active_row->color .";width: 10px;height:10px;float:left;margin:5px 10px 0px 10px;'> </div>
<span id='myBtn' style='color:orange'> ". $active_row->title ."<span id='GoogleADD' style='float:right; color:orange; text-decoration:underline'> Add </span> </span> <span id='end' style='float:right; margin-right: 10px'>". $endDateString ."</span> <span style='float:right'> - </span> <span id='start' style='float:right; margin-left:10px'> ". $newDateString ." </div> <!-- Trigger/Open The Modal -->
<!-- The Modal -->
<div id='myModal' class='modal'>
<!-- Modal content -->
<div class='modal-content'>
<div class='modal-header'>
<span class='close'>×</span>
<h2 style='text-align:center'>Podrobnosti o akci</h2>
</div>
<div class='modal-body'>
<p> Název akce: <b>". $active_row->title ."</b> </p>
<p> Podrobnosti: <b>". $active_row->description ." </b> </p>
<p> Začátek akce: <b>". $newDateString ." </b> </p>
<p> Konec akce: <b>". $endDateString ." </b> </p>
<p> Přidat akci do vašeho Google Kalendáře: <b style='color: orange; text-decoration: underline'> ADD ME! </b> </p>
</div>
<div class='modal-footer'></div>
</div>
</div>");
然后我有一个脚本,当有人点击它们时,我想在其中显示它们。我在这里检查“第三”行的 ID (span id="myBtn")。
这是我的 jQuery 脚本。
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById('myBtn');
// Get the <span> element that closes the modal
var span = document.getElementsByClassName('close')[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = 'block';
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = 'none';
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
}
</script>"
你能帮帮我吗?非常感谢!
【问题讨论】:
-
“id”属性应该是唯一的 - 有两个或多个具有相同 id 的元素是错误的。
-
@Pointy 所说的——如果你使用类,你可以使用
getElementsByClassName() -
我试过了,但我不能做一个唯一的 id...
-
那不是 jquery。那只是javascript。如果您使用的是 jquery,您将使用
$('#myModal)而不是输入完整的getWhatever()函数名称。 -
说你不能做一个唯一的ID意味着(a)你需要找到一种方法;或 (b) 您需要使用 ID 以外的其他内容。两个重复的 ID 违反了 w3c 标准——这是有充分理由的。
标签: javascript php jquery foreach