简体   繁体   English

element.html()在DOM准备就绪时返回空

[英]element.html() return empty on DOM ready

I am working on a countdown timer, using http://keith-wood.name/countdown.html . 我正在使用http://keith-wood.name/countdown.html制作一个倒数计时器。

My code looks like this, 我的代码看起来像这样,

$('.clock').countdown({until:new Date(2012, 11, 31), format: 'odHMS', onTick:watchCountdown, tickInterval:5, layout:
    '<div id="timer">' +
        '<div id="timer_labels">'+
            '<div id="timer_months_label" class="timer_labels">Months</div>'+
            '<div id="timer_weeks_label" class="timer_labels">Days</div>'+
            '<div id="timer_days_label" class="timer_labels">Hours</div>'+
            '<div id="timer_minutes_label" class="timer_labels">Minutes</div>'+
        '</div>'+
        '<div id="timer_months" class="timer_numbers"><span class="divide"></span><span>{onn}</span></div>'+
        '<div id="timer_days" class="timer_numbers"><span class="divide"></span><span>{dnn}</span></div>'+
        '<div id="timer_hours" class="timer_numbers"><span class="divide"></span><span>{hnn}</span></div>'+
        '<div id="timer_minutes" class="timer_numbers"><span class="divide"></span><span>{mnn}</span></div>'+
    '</div>'
});

As you onTick I call a function that looks like this, 当你onTick我调用一个看起来像这样的函数,

function watchCountdown(periods) { 
var $div = $("#monitor");
var html = $div.html();
var checking = setInterval(function() {
    var newhtml = $div.html();
    if (html !== newhtml) {
        myCallback($div);
    }
},100);
$('#monitor').html('<div id="timer_minutes" class="timer_numbers"><span class="divide"></span><span class="minutes">' + periods[5] + '</span></div>'); 

} }

In the function above there is a check for html != newhtml however html is returning as an empty variable evening though there is HTML in the #monitor element. 在上面的函数中有一个检查html!= newhtml但是html作为一个空变量晚上返回,尽管#monitor元素中有HTML。 How can I make it realise there is content to check against? 如何让它意识到有内容需要检查?

You don't understand what is DOM ready, it fires only once, when the DOM is fully loaded and rendered from the server. 当DOM完全加载并从服务器呈现时,您无法理解DOM准备好了什么,它只触发一次。

If you change the DOM with javascript, it has no effect on the DOM ready event. 如果使用javascript更改DOM,则它对DOM ready事件没有影响。

$('#monitor').html('<div id="timer_minutes" class="timer_numbers"><span class="divide"></span><span class="minutes">' + periods[5] + '</span></div>');

The code above will not cause delay or trigger again the DOM ready event. 上面的代码不会导致延迟或再次触发DOM就绪事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM