0
下面是我寫的數據庫中顯示圖像的代碼當我點擊圖像顯示爲模式時我從這個目標寫代碼,但是當我運行代碼時它只能用於最後一項請建議我任何想法或解決方案爲什麼bootstrap模式只適用於一個項目
引導和PHP
<?php
$stmt = $DB_con->prepare('SELECT ID, title, content, img FROM shop where lang="en" ORDER BY ID DESC');
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<img id="myImg" src="admin/view/pages/en/shop/user_images/<?php echo $row['img']; ?>" alt="<?php echo $content; ?>" width="300" height="200">
<?php
}
}
?>
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
的JavaScript
<script>
var modal = document.getElementById('myModal');
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
//Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
//When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>
你不能使用相同的ID爲多個圖像標籤,ID應該是唯一的 – Javed
請問你能告訴我 –
更改你的'id =「myImg」'爲'id =「myImg <?php echo $ row [「ID」]?>「'並且只添加像這樣的class屬性:'class =」myImg「'? –