jQuery 浏览器检测似乎不起作用

发布于 2024-08-12 16:56:44 字数 1311 浏览 10 评论 0原文

由于 IE 无法淡出透明 PNG,我决定更改我的功能,以便在用户使用 IE 时仅显示 PNG,但如果用户使用任何其他浏览器则淡出它们。下面的函数在 IE 中运行良好,并且达到了我的预期,但在任何其他浏览器(即 Firefox、Safari)中它没有执行任何操作,我是否遗漏了某些内容或者是否存在语法错误?

    $('#content2 a').click(function(){

 if($.browser.msie){

        var toLoad = $(this).attr('href')+' #content';
        $('#content').show(loadContent);

        window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
        function loadContent() {
            $('#content').load(toLoad,'',showNewContent())
        }
        function showNewContent() {
            $('#content').show();
        }

        return false;

 }else{

        var toLoad = $(this).attr('href')+' #content';
        $('#content').fadeOut('slow',loadContent);
        $('#load').remove();
        $('#wrapper').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('slow');
        window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
        function loadContent() {
            $('#content').load(toLoad,'',showNewContent())
        }
        function showNewContent() {
            $('#content').fadeIn('slow',hideLoader());
        }
        function hideLoader() {
            $('#load').fadeOut('slow');
        }
        return false;

 };

});

Due to IE's inability to fade transparent PNG's I have decided to change my function to simply show PNG's if the user is using IE but to fade them if they are using any other browser. The function below works fine in IE and does what I expect it to but in any other browser i.e. Firefox, Safari it doesn't do anything, am I missing something or do I have a syntax error?

    $('#content2 a').click(function(){

 if($.browser.msie){

        var toLoad = $(this).attr('href')+' #content';
        $('#content').show(loadContent);

        window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
        function loadContent() {
            $('#content').load(toLoad,'',showNewContent())
        }
        function showNewContent() {
            $('#content').show();
        }

        return false;

 }else{

        var toLoad = $(this).attr('href')+' #content';
        $('#content').fadeOut('slow',loadContent);
        $('#load').remove();
        $('#wrapper').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('slow');
        window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
        function loadContent() {
            $('#content').load(toLoad,'',showNewContent())
        }
        function showNewContent() {
            $('#content').fadeIn('slow',hideLoader());
        }
        function hideLoader() {
            $('#load').fadeOut('slow');
        }
        return false;

 };

});

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

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

发布评论

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

评论(2

计㈡愣 2024-08-19 16:56:44

大多数回调都是这样写的:

$('#content').load(toLoad,'',showNewContent())

其中表示调用 showNewContent,并将其返回值传递给 jQuery。

您的意思是:

$('#content').load(toLoad, '', showNewContent);

但这里也有一个潜在的问题:

}else{
    ...
    $('#content').fadeOut('slow',loadContent);
    ...
    function loadContent() {

function 语句放在 else 或除另一个函数之外的任何其他块中实际上是不合法的。这是因为 function 语句的神奇之处在于,它允许您在上面定义函数的代码中使用函数,只有在函数开始运行之前函数定义已修复的情况下,该函数才能起作用。如果定义位于条件块内,则不会发生这种情况。

正如 JavaScript 中常见的流血现象一样,浏览器不会告诉您错误,而是会让您侥幸逃脱,但结果不一致且奇怪。这是一个明显矛盾的测试用例:

x();
var isgood= Math.random()*2>=1;

if (isgood) {
    function x() {
        alert('Good!');
    }
} else {
    function x() {
        alert('Bad!');
    }
}

此示例代码尝试在该事件实际发生之前调用一个依赖于抛硬币结果的函数。光这么说应该是语法错误,但每个浏览器都允许这样做。 Mozilla 至少让 x 保持 undefined 直到到达函数语句,因此初始的 x() 将导致错误。然而,在 IE、Webkit 和 Opera 中,第二个 function 语句“获胜”,并且它总是警告“Bad!”。浏览器不好!

每当您需要定义发生更改的函数时,请使用内联函数表达式而不是函数语句来避免此问题。尽管在这种情况下,您可以通过将 IE 的 speed 设置为 0 使其立即回调来解决淡入淡出问题,这使整个事情变得更加简单:

$('#content2 a').click(function() {
    var speed= $.browser.msie? 0 : 'slow';
    $('#content').fadeOut(speed, function() {
        $('#content').load($(this).attr('href')+' #content', '', function() {
            $('#content').fadeIn(speed);
        });
    });
});

Most of your callbacks are written like this:

$('#content').load(toLoad,'',showNewContent())

Which says to call showNewContent, and pass its return value to jQuery.

You mean:

$('#content').load(toLoad, '', showNewContent);

But you also have a potential problem here:

}else{
    ...
    $('#content').fadeOut('slow',loadContent);
    ...
    function loadContent() {

It's not actually legal to put a function statement inside an else or any other block than another function. This is because the magic of the function statement that allows you to use a function in code above where you define it can only work if the function definition is fixed before the start of the function is run. That can't happen if the definition is inside a conditional block.

As bleeding usual with JavaScript, instead of telling you about the error, browsers will let you get away with it but with inconsistent and weird results. Here's an obviously paradoxical test case:

x();
var isgood= Math.random()*2>=1;

if (isgood) {
    function x() {
        alert('Good!');
    }
} else {
    function x() {
        alert('Bad!');
    }
}

This example code tries to call a function that's dependent on the result of a coin toss before that event has actually taken place. It should be a syntax error just to say this, but every browser allows it. Mozilla at least leaves x holding undefined until a function statement is reached so the initial x() will cause an error. In IE, Webkit and Opera, however, the second function statement ‘wins’ and it always alerts “Bad!”. Bad browsers!

Avoid this issue by using inline function expressions instead of function statements any time you need a function whose definition changes. Although in this case, you could get around the fading problem by just using a speed of 0 for IE to make it callback instantly, which makes the whole thing much simpler:

$('#content2 a').click(function() {
    var speed= $.browser.msie? 0 : 'slow';
    $('#content').fadeOut(speed, function() {
        $('#content').load($(this).attr('href')+' #content', '', function() {
            $('#content').fadeIn(speed);
        });
    });
});
<逆流佳人身旁 2024-08-19 16:56:44

也许你应该添加“;”行尾:

$('#content').load(toLoad,'',showNewContent());

loadContent() 的两个定义中。

Maybe you should add the ';' at the end of the line:

$('#content').load(toLoad,'',showNewContent());

in both definitions of loadContent().

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