jQuery 浏览器检测似乎不起作用
由于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数回调都是这样写的:
其中表示调用 showNewContent,并将其返回值传递给 jQuery。
您的意思是:
但这里也有一个潜在的问题:
将
function
语句放在else
或除另一个函数之外的任何其他块中实际上是不合法的。这是因为function
语句的神奇之处在于,它允许您在上面定义函数的代码中使用函数,只有在函数开始运行之前函数定义已修复的情况下,该函数才能起作用。如果定义位于条件块内,则不会发生这种情况。正如 JavaScript 中常见的流血现象一样,浏览器不会告诉您错误,而是会让您侥幸逃脱,但结果不一致且奇怪。这是一个明显矛盾的测试用例:
此示例代码尝试在该事件实际发生之前调用一个依赖于抛硬币结果的函数。光这么说应该是语法错误,但每个浏览器都允许这样做。 Mozilla 至少让
x
保持undefined
直到到达函数语句,因此初始的x()
将导致错误。然而,在 IE、Webkit 和 Opera 中,第二个function
语句“获胜”,并且它总是警告“Bad!”。浏览器不好!每当您需要定义发生更改的函数时,请使用内联函数表达式而不是函数语句来避免此问题。尽管在这种情况下,您可以通过将 IE 的
speed
设置为 0 使其立即回调来解决淡入淡出问题,这使整个事情变得更加简单:Most of your callbacks are written like this:
Which says to call showNewContent, and pass its return value to jQuery.
You mean:
But you also have a potential problem here:
It's not actually legal to put a
function
statement inside anelse
or any other block than another function. This is because the magic of thefunction
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:
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
holdingundefined
until a function statement is reached so the initialx()
will cause an error. In IE, Webkit and Opera, however, the secondfunction
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:也许你应该添加“;”行尾:
在
loadContent()
的两个定义中。Maybe you should add the ';' at the end of the line:
in both definitions of
loadContent()
.