部分javascript在IE8中不起作用

部分javascript在IE8中不起作用,javascript,internet-explorer-8,Javascript,Internet Explorer 8,下面的querySelector功能在IE8中不起作用,但在IE11中也起作用 代码: $(document).ready( function() { $(window).load( function() { var fiveMinutes = 60 * 15, display = document

下面的querySelector功能在IE8中不起作用,但在IE11中也起作用

代码:

    $(document).ready(
        function() {

            $(window).load(
                    function() {
                        var fiveMinutes = 60 * 15, display = document
                                .querySelector('#time');
                        startTimer(fiveMinutes, display);
                    });

            function startTimer(duration, display) {
                var timer = duration, minutes, seconds;
                setInterval(function() {
                    minutes = parseInt(timer / 60, 10)
                    seconds = parseInt(timer % 60, 10);

                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;

                    display.textContent = minutes + ":" + seconds;

                    if (--timer < 0) {
                        timer = duration;
                    }
                }, 1000);
            }
$(文档)。准备好了吗(
函数(){
$(窗口)。加载(
函数(){
变量五分钟=60*15,显示=文档
.querySelector(“时间”);
startTimer(五分钟,显示);
});
功能启动计时器(持续时间、显示){
var定时器=持续时间,分钟,秒;
setInterval(函数(){
分钟=parseInt(计时器/60,10)
秒=parseInt(计时器%60,10);
分钟=分钟<10?“0”+分钟:分钟;
秒=秒<10?“0”+秒:秒;
display.textContent=分钟+“:”+秒;
如果(--定时器<0){
定时器=持续时间;
}
}, 1000);
}
HTML代码:

   <div >
    <div id ="timer">
        Next Refresh will be in <span id="time">05:00</span> minutes!
    </div>
    <div>

下次刷新将在05:00分钟后进行!
IE8不支持
HTMLElement#textContent

您正在使用jQuery,让我们使用jQuery来解决这个问题——它最初创建的原因有一半是为了处理浏览器不一致性(只有一半,这不再是它存在的主要原因)

最小的改变就是改变

display.textContent = minutes + ":" + seconds;


这段代码还存在一些其他问题。特别是,您正沦为-声明变量的牺牲品!您可以使用jQuery来查找元素,而不是使用
querySelector
。因此:

$(document).ready(
    function() {

        $(window).load(
            function() {
                var fiveMinutes = 60 * 15,
                    display = $("#time");            // Use jQuery to look this up
                startTimer(fiveMinutes, display);
            });

        function startTimer(duration, display) {
            var timer = duration,
                minutes, seconds;
            setInterval(function() {
                // Note the variable declarations
                var minutes = parseInt(timer / 60, 10)
                var seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;

                display.text(minutes + ":" + seconds);      // Use text() to set the text

                if (--timer < 0) {
                    timer = duration;
                }
            }, 1000);
        }
    }
);
$(文档)。准备好了吗(
函数(){
$(窗口)。加载(
函数(){
变量五分钟=60*15,
display=$(“#time”);//使用jQuery查找
startTimer(五分钟,显示);
});
功能启动计时器(持续时间、显示){
var定时器=持续时间,
分,秒;
setInterval(函数(){
//注意变量声明
var分钟=parseInt(计时器/60,10)
var seconds=parseInt(计时器%60,10);
分钟=分钟<10?“0”+分钟:分钟;
秒=秒<10?“0”+秒:秒;
display.text(分钟+“:“+秒);//使用text()设置文本
如果(--定时器<0){
定时器=持续时间;
}
}, 1000);
}
}
);
如果您使用来查找兼容性,它会说IE8可以使用
查询选择器
,但是如果您继续使用,您会发现
查询选择器
在IE8中受到限制。请改用
文档.getElementById
$(document).ready(
    function() {

        $(window).load(
            function() {
                var fiveMinutes = 60 * 15,
                    display = $("#time");            // Use jQuery to look this up
                startTimer(fiveMinutes, display);
            });

        function startTimer(duration, display) {
            var timer = duration,
                minutes, seconds;
            setInterval(function() {
                // Note the variable declarations
                var minutes = parseInt(timer / 60, 10)
                var seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;

                display.text(minutes + ":" + seconds);      // Use text() to set the text

                if (--timer < 0) {
                    timer = duration;
                }
            }, 1000);
        }
    }
);