Chrome 中真正神秘的 DOM 问题

发布于 2024-11-09 06:48:45 字数 1695 浏览 0 评论 0原文

请耐心等待,这将非常难以解释,而且我无法真正展示太多代码,因为我不确定是否要转储所有将在主要国家网站上使用的开发代码。

我有四个 LI 标签,在 $(window).load() 上,它们被克隆到 DIV 中,然后使用 $.each() 循环获取 DIV 的高度。我遇到的问题是,在各种操作系统上的大多数浏览器中,高度都是一致的,除了 Linux 和 Mac 上的 Chrome 之外。我会详细说明一下。

每个 LI 包含一些文本和一些其他 div,基本上只是一般内容。我使用 $.contents().clone() 克隆每个 LI 并将其放入 DIV 中,其样式不同。这就是该特定部分的工作方式,并且无法更改。

好的,jQuery 代码时间:

$(window).load(function() {

    $('.carousel-thumb').each(function(i) {
        var $story = $(this).contents().clone();

        // Remove all <script> tags before appending, otherwise we're looking
        // for trouble
        $('script', $story).remove();

        $story.appendTo('#featured-story')
            .wrapAll('<div class="carousel-story" />');
    });

    // Fix the sizing on the featured story container
    $('.carousel-story').each(function(i) {
        console.log('story ' + i + ' = ' + $(this).height());
        if ($(this).height() > storyMaxHeight)
            storyMaxHeight = $(this).height();
    });
    /*$('.carousel-story').height(storyMaxHeight);
    $('#featured-story').height(storyMaxHeight);*/
});

我将回答由此可能出现的一个潜在问题。删除脚本标签会发生什么?原始的LI标签包含各种内容,包括内联脚本。我注意到当我克隆此内容时,它对页面结构造成了严重破坏。不确定脚本是否再次执行或什么,但删除脚本(特别是因为它们已经被执行)解决了问题。

现在,看看这个有趣的花絮。该图左侧包含 Windows Chrome,右侧包含 Linux Chrome。请注意,Linux/Mac Chrome 都会出现异常行为。

左:Windows Chrome;右:Linux Chrome

这不是很奇怪吗?在脚本执行过程中,Linux 中的高度完全错误。然而,在一切完成后,我手动请求第三个 div 的高度,它正确报告了高度!然而,Windows Chrome 以及所有平台上的 Firefox(甚至 IE7)都表现出左侧(正确)的行为。

在clone()调用中是否有一些奇怪的延迟?克隆一堆内容是否需要一些时间,并且 jQuery 脚本继续执行并因此获取错误的高度?有没有办法暂停,以确保所有内容都已正确克隆?

谢谢。我真的为这件事抓狂了。

Bear with me, this is going to be extremely difficult to explain, and I can't really show much code because I'm not sure I wanna dump all this development code which is going to be used on a major national web site.

I have four LI tags that, upon $(window).load(), are being cloned into DIVs, and then the heights of the DIVs are fetched with an $.each() loop. The problem I'm running into is, in most browsers on various OS's, the heights are consistent, except Chrome on Linux and Mac. I'll elaborate a bit more.

Each LI contains some text and some other divs, just general content basically. And I'm using $.contents().clone() to clone each LI and put it in a DIV, where it is styled differently. This is how that particular part works and it cannot change.

OK, jQuery code time:

$(window).load(function() {

    $('.carousel-thumb').each(function(i) {
        var $story = $(this).contents().clone();

        // Remove all <script> tags before appending, otherwise we're looking
        // for trouble
        $('script', $story).remove();

        $story.appendTo('#featured-story')
            .wrapAll('<div class="carousel-story" />');
    });

    // Fix the sizing on the featured story container
    $('.carousel-story').each(function(i) {
        console.log('story ' + i + ' = ' + $(this).height());
        if ($(this).height() > storyMaxHeight)
            storyMaxHeight = $(this).height();
    });
    /*$('.carousel-story').height(storyMaxHeight);
    $('#featured-story').height(storyMaxHeight);*/
});

I'll answer one potential question that might arise from this. What's with the removal of the script tags? The original LI tags contain various content, including inline scripts. I noticed when I cloned this content, it wreaked havoc on the page structure. Not sure if the scripts were executing again or what, but removing the scripts (especially since they'd already been executed) solved the problem.

Now, check out this interesting tidbit. This graphic contains Windows Chrome on the left, and Linux Chrome on the right. Note that Linux/Mac Chrome both misbehave.

Left: Windows Chrome; Right: Linux Chrome

Isn't THAT odd? During the execution of the script, the heights are completely wrong in Linux. Yet, after everything is completely done and I manually request the height of the third div, it reports the height properly! Yet the behavior on the left (correct) is exhibited by Windows Chrome as well as Firefox on all platforms, even IE7.

Is there some weird delay in the clone() call? Does cloning a bunch of content take some time, and the jQuery script continues to execute and hence grabs the wrong heights? Is there a way to pause, to make sure all content has been cloned properly?

Thanks. I'm really pulling my hair out over this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-11-16 06:48:45

显然这是 Chrome 和 Safari 中随机出现的一个错误。这些浏览器有时会在所有图像/媒体加载完成之前触发 load() 函数。有人推荐此修复/黑客:

jQuery(window).load(function(){
  if (jQuery.browser.safari && document.readyState != 'complete'){
    // chrome / safari will trigger load function before images are finished
    // check readystate in safari browser to ensure images are done loading
    setTimeout( arguments.callee, 100 );
    return;
  }
  // do things you want to do
});

来源:http://javascript.livejournal.com/169501.html

Apparently it's a bug in Chrome and Safari that surfaces randomly. These browsers will sometimes trigger the load() function before all images/media are done loading. One person recommends this fix/hack:

jQuery(window).load(function(){
  if (jQuery.browser.safari && document.readyState != 'complete'){
    // chrome / safari will trigger load function before images are finished
    // check readystate in safari browser to ensure images are done loading
    setTimeout( arguments.callee, 100 );
    return;
  }
  // do things you want to do
});

Source: http://javascript.livejournal.com/169501.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文