[英]iOS unable to manipulate jQuery DOM changes on the first page load
我在iOS上的Safari上遇到了一個奇怪的問題。 我正在使用jQuery操縱幾個div,移動它們,包裝它們並賦予它們動態高度。 在每個瀏覽器上,這些更改在Safari上的首頁加載過程中都可以正常運行,有時是,有時不是。
基本上是在Safari中,它會加載頁面而不進行任何更改,然后,如果我不斷刷新它,它將永遠無法工作,但是如果我在地址欄中重新輸入網址,然后點擊“ enter”,那么它將可以正常工作。
現在,我解決了它,將觸發那些DOM操作的函數包裝在setTimeout函數中,並給它提供了400的延遲。所以現在它在觸發該函數之前要等待400ms,這樣它才能起作用,但是我不喜歡它許多。 我想知道你們是否知道更好的系統。
我的代碼:
$('.wrap-services, .wrap-banner-cta, .wrap-logos, footer').wrapAll('<div class="fixer"></div>');
$('.fixer').insertAfter('.content');
function getHeight() {
var imgOffset = $('.wrap-hero-home picture.splash-main img').offset().top;
var fixerHeight = $('.fixer').height();
imgHeight = $('.wrap-hero-home picture.splash-main img').height();
$('.fixer').css('top', (imgOffset + imgHeight) + 50);
$('.container').height((imgOffset + imgHeight + fixerHeight) + 50);
}
//this is what I use now to prevent that issue
setTimeout(getHeight, 400);
謝謝
從技術上講,不推薦使用jQuery的bind方法,因此您可能應該使用on
,就像他們建議的那樣:
$(window).on('load', function(){
//Do something
});
請參閱此頁面中與棄用相關的注釋: http : //api.jquery.com/bind/
如果您使用的是舊版本的jQuery,我想沒關系,那么請繼續進行bind
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.