0

我正在尝试在 Angular 单页应用程序中的 php 页面上弹出一个 javascript 模式窗口。此页面在初始 index.html 之后加载。html标签写成:

<html ng-app="myApp" class="ng-scope">

...并且加载的页面中的内容存储在:

<div ng-view="" class="ng-scope">

特定 php 页面中的 javascript 引发了错误。这是完整的 php 页面,包括底部的 javascript:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div id="myModal" class="modal">

    <!-- Modal content -->
        <div class="modal-content">
        <span class="close">&times;</span>
        <img id="popup_img" src="#">
         </div>

    </div>
    <div id="screenings">
        <?php
            $db = mysqli_connect("localhost", "root", "", "database");
            $sql = "SELECT * FROM screenings";
            $result = mysqli_query($db, $sql);
            while ($row = mysqli_fetch_array($result)){
                echo "<div id='img_div' onClick='btn()'>";
                    echo "<img id='img_screenings' class='modal_img' src='images/".$row['image']."' >";;
                    echo "<p id='screenings_p' align='center'>" .$row['venue']."</p>";
                    echo "<p id='location_p' align='center'>" .$row['location']."</p>";
                    echo "<p id='date_p' align='center'>".date('F j, Y',strtotime($row['date']))."</p>";
                echo "</div>";
            }
        ?>
    </div>
<script>

$document.ready(function(){
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
//var btn = document.getElementsByClassName("modal_img");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

$("img").on("click", function() {
var source = ( $(this).attr("src") );
//alert(source);
//$('#modal_popup').text(source);
$('#popup_img').prop('src', this.src);
});

// When the user clicks the button, open the modal 
function btn() {
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>
</body>

具体来说,我在 Chrome 控制台中收到此错误:“未捕获的 ReferenceError:btn 未在 HTMLDivElement.onclick 中定义”

4

0 回答 0