Javascript 为什么我的代码在Chrome/Safari中完全兼容,在FireFox中有些兼容,但在InternetExplorer8中几乎不兼容?
我写了一些jQuery代码,在Chrome/Safari中运行得很好,在Firefox中运行得很好,在IE8中几乎没有 这是为什么?我如何修复它 以下是我的代码,如果您想了解IE或Firefox中可能出现问题的原因,您可以在以下网址查看正在运行的代码:Javascript 为什么我的代码在Chrome/Safari中完全兼容,在FireFox中有些兼容,但在InternetExplorer8中几乎不兼容?,javascript,jquery,internet-explorer,cross-browser,Javascript,Jquery,Internet Explorer,Cross Browser,我写了一些jQuery代码,在Chrome/Safari中运行得很好,在Firefox中运行得很好,在IE8中几乎没有 这是为什么?我如何修复它 以下是我的代码,如果您想了解IE或Firefox中可能出现问题的原因,您可以在以下网址查看正在运行的代码: 我理解一些事情,比如它希望我在回调周围添加{},但它为什么想要!==而不是在某些情况下,等等?我解决了三个问题中的两个: 当设置整个页面的动画(如滚动到内容)时,必须以html和body标记为目标,如:$'html,body'。以前我只有$'bo
我理解一些事情,比如它希望我在回调周围添加{},但它为什么想要!==而不是在某些情况下,等等?我解决了三个问题中的两个: 当设置整个页面的动画(如滚动到内容)时,必须以html和body标记为目标,如:$'html,body'。以前我只有$'body',所以代码在IE中不起作用。Chrome需要'body',而IE需要'html',所以总的来说你需要包括这两个 而且,注释是从ajax调用中剥离出来的,所以我的flash嵌入,在IE中使用html注释的特殊条件,在IE中使用错误的标记。这就是为什么IE中不播放视频的原因
最后一个仍然存在的问题是:firefox无法在打开一个盒子后正确地重新组织我网站上的盒子。谁知道为什么 是的,jQuery与所有当前浏览器都是跨浏览器兼容的。不,它不会使您编写的所有代码自动跨浏览器兼容。除非您详细地告诉我们您遇到了什么问题,并亲自尝试调试,否则很难帮助您。我使用的是最新版本IE8@deceze我的代码大部分完全是用jQuery编写的。。。即使这样,在某些浏览器中仍然可以不工作?我几乎没有任何自定义代码,如果有的话。旁注:你不需要e.preventDefault;并返回false;。一个或另一个就足够了。IE对无效js的容忍度要低得多。我会先修复你发布的所有警告,然后再尝试其他东西。补充:这可能是因为您多次声明变量。
jQuery(".wrap").append("<img src='" + base + "/wp-content/themes/shaken-grid/images/ajax-loader.gif' id='ajax-loader' style='position: fixed; margin-left: 50%; left:-154px; top: 30%; display: none;' />", function() {
jQuery("#ajax-loader").hide();
});
var $mainContent = jQuery("#grid"),
$ajaxSpinner = jQuery("#ajax-loader"),
$clicked_item,
$target_open,
$target_close,
$element;
jQuery('a.ajax_trigger_title, a.more-link, a.ajax_trigger_thumbnail').live('click', function(event) {
var $element = jQuery(this);
if ( $element.hasClass('ajax_trigger_title') ) {
var $post_box = $element.parent().parent();
$clicked_item = jQuery( '#' + $post_box.attr('id') + ' .ajax_trigger_title' );
}
if ( $element.hasClass('ajax_trigger_thumbnail') ) {
var $post_box = $element.parent().parent();
$clicked_item = jQuery( '#' + $post_box.attr('id') + ' .ajax_trigger_thumbnail' );
}
if ( $element.hasClass('more-link') ) {
var $post_box = $element.parent().parent().parent().parent();
$clicked_item = jQuery( '#' + $post_box.attr('id') + ' .more-link' );
}
$target_open = jQuery( '#' + $post_box.attr('id') );
var path = jQuery(this).attr('href').replace(base, '');
jQuery.address.value(path); // changes the address deep link (the part after the /#/)
// Default action (go to link) prevented for comment-related links (which use onclick attributes)
event.preventDefault();
return false;
});
jQuery('a.ajax_trigger_close').live('click', function(event) {
var $element = jQuery(this);
var $post_box = $element.parent();
$clicked_item = jQuery( '#' + $element.attr('id') );
$target_close = jQuery( '#' + $post_box.attr('id') );
var path = jQuery(this).attr('href').replace(base, '');
jQuery.address.value(path); // changes the address deep link (the part after the /#/)
// Default action (go to link) prevented for comment-related links (which use onclick attributes)
event.preventDefault();
return false;
});
/*jQuery('a#home_link').live('click', function(event) {
var $element = jQuery(this);
var $post_box = $element.parent();
$clicked_item = jQuery( '#' + $element.attr('id') );
var path = jQuery(this).attr('href').replace(base, '');
jQuery.address.value(path); // changes the address deep link (the part after the /#/)
// Default action (go to link) prevented for comment-related links (which use onclick attributes)
event.preventDefault();
return false;
});*/
function dim_box($target_box, callback) {
$target_box.animate({ opacity: "0.1" }, function() {
if (callback) callback();
});
}
function undim_box($target_box, callback) {
$target_box.animate({ opacity: "1" }, function() {
if (callback) callback();
});
}
function show_loader(position, callback) {
$ajaxSpinner.fadeIn(function() {
if (callback) callback();
});
}
function hide_loader(callback) {
$ajaxSpinner.fadeOut(function() {
if (callback) callback();
});
}
function open_box($target_box, $target_path, $target_content, masonize_on_open, callback) {
$target_box.find('.opened_view')
.load(base + $target_path + ' ' + $target_content, function() {
$target_box.find('.closed_view').addClass('hidden');
$target_box.find('.thumbnail_wrapper').addClass('hidden');
$target_box.find('.ajax_trigger_title').addClass('opened_post_title');
$target_box.width(660);
$target_box.append('<a class="ajax_trigger_close" id="close_' + $target_box.attr('id') + '" href="' + base + '/">Close</a>');
if (masonize_on_open) masonize();
if (callback) callback();
});
}
function close_box($target_box, masonize_on_close, callback) {
$target_box.find('.opened_view').html('');
$target_box.width(310);
$target_box.find('.closed_view').removeClass('hidden');
$target_box.find('.thumbnail_wrapper').removeClass('hidden');
$target_box.find('.ajax_trigger_title').removeClass('opened_post_title');
$target_box.find('a.ajax_trigger_close').remove();
if (masonize_on_close) masonize();
if (callback) callback();
}
function scroll_to_content($target, duration, top_margin, callback) { //scrolls the page to the $target. $target can be a jQuery object or the number of pixels to scroll from the top.
if ($target instanceof jQuery) {
jQuery('body').animate({
scrollTop: $target.offset().top - top_margin
}, duration, function() {
if (callback) callback();
});
}
//three ways to check for an integer below:
else if ($target === parseInt($target,10)) { //else if integer
//else if ( (typeof($target) == 'number') && ($target.toString().indexOf('.') == -1) ) { //else if integer
//else if ( !isNaN(parseInt($target)) ) { //else if integer
jQuery('body').animate({
scrollTop: $target
}, duration, function() {
if (callback) callback();
});
}
}
jQuery.address.change(function(event) {
if (event.value != '/' && $clicked_item) {
if ($target_close) { //if not first item to be opened then close previously opened item.
show_loader(0, function() {
open_box($target_open, event.value, '.entry-content', false, function() {
close_box($target_close, true);
hide_loader(function() {
scroll_to_content($target_open, 360, 20);
});
$target_close = $target_open;
});
});
}
else { //otherwise just open target item since it is the first item to be opened.
show_loader(0, function() {
open_box($target_open, event.value, '.entry-content', true, function() {
hide_loader(function() {
scroll_to_content($target_open, 360, 20);
});
$target_close = $target_open;
});
});
}
}
else if ( event.value == '/' && $clicked_item ) {
if ( $clicked_item.hasClass('ajax_trigger_close') && $clicked_item.attr('id') != 'home_link' ) {
close_box($target_close, true);
scroll_to_content(0, 360);
}
}
});
Error:
Problem at line 21 character 23: '$post_box' is already defined.
var $post_box = $element.parent().parent();
Problem at line 25 character 23: '$post_box' is already defined.
var $post_box = $element.parent().parent().parent().parent();
Problem at line 28 character 34: '$post_box' used out of scope.
$target_open = jQuery( '#' + $post_box.attr('id') );
Problem at line 72 character 23: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 78 character 23: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 84 character 23: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 90 character 23: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 102 character 35: Expected '{' and instead saw 'masonize'.
if (masonize_on_open) masonize();
Problem at line 103 character 27: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 114 character 28: Expected '{' and instead saw 'masonize'.
if (masonize_on_close) masonize();
Problem at line 115 character 19: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 123 character 27: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 133 character 27: Expected '{' and instead saw 'callback'.
if (callback) callback();
Problem at line 139 character 21: Expected '!==' and instead saw '!='.
if (event.value != '/' && $clicked_item) {
Problem at line 162 character 27: Expected '===' and instead saw '=='.
else if ( event.value == '/' && $clicked_item ) {
Problem at line 163 character 87: Expected '!==' and instead saw '!='.
if ( $clicked_item.hasClass('ajax_trigger_close') && $clicked_item.attr('id...
Implied global: jQuery 2,3,6,7,13,15,39,41,119,138, base 2,30,46,96,101, masonize 102,114