0

在我的状态页面上,我有一个名为 的 div id errorBarGoesHere。我有一个简单的 AJAX 调用,它每 10 秒运行一次,以检查它是否应该显示错误栏。一切正常,错误栏也在我想要的时候显示,等等。

现在我只想能够关闭命令错误栏。但是我制作的按钮无法访问或查看任何其他 JS 功能,因为它位于不同的页面上。我检查了网页上的源代码,当错误栏明显显示在页面上时,甚至没有看到错误栏的代码;只是errorBarGoesHere空白的 div 标签。

有没有办法可以使用我制作的那个按钮来清除间隔?代码如下。

Javascript

var setErrorBarId = window.setInterval(function(){runErrorBarAjax();}, 10000);

function runErrorBarAjax() {
    var xmlhttp = getAjaxObject();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            processErrorBarResponse(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", "lib/errorBar.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send();
}
function processErrorBarResponse(response){
    document.getElementById(\'errorBarGoesHere\').innerHTML = response;
}

错误栏.php

session_start(); // Start the session from here.

if(isset($_SESSION['errorBarError'])){
    echo '
    <link rel="stylesheet" type="text/css" href="main.css" media="screen" />
    <div id="errorBar">
        <div id="innertube">
            <h2><center>
                <div id="errorBarCaution"><img src="../images/caution.png" width="18" height="18"></div>'.$_SESSION['errorBarError'].'
            </center></h2>
            <input type="image" src="../images/closeButton.png" width="20" height="20" onclick="clearInt(setErrorBarId);">
        </div>
    </div>
    ';
    unset($_SESSION['errorBarError']);
}
4

1 回答 1

0

最好的方法是使用jQuery。您的文档可以是..

<html>
<head>
    <link rel="stylesheet" type="text/css" href="main.css" media="screen" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
    <div id="errorBarGoesHere"></div>
    <input id="remove" type="image" src="../images/closeButton.png" width="20" height="20" />
    <script type="text/javascript">
        $(document).ready(function() {
            var setErrorBarId = window.setInterval(function(){
                $("#errorBarGoesHere").load("lib/errorBar.php");
            }, 10000);
            $("#remove").on('click', function() {
                window.clearInterval(setErrorBarId);
                $(this).remove();
            });
         });
     </script>
</body>
</html>

错误栏.php..

session_start(); // Start the session from here.

if(isset($_SESSION['errorBarError'])){
    echo '
    <div id="errorBar">
        <div id="innertube">
            <h2><center>
                <div id="errorBarCaution"><img src="../images/caution.png" width="18" height="18"></div>'.$_SESSION['errorBarError'].'
            </center></h2>
        </div>
    </div>
    ';
    unset($_SESSION['errorBarError']);
}
于 2013-09-17T23:06:24.733 回答