【发布时间】:2011-09-25 07:15:21
【问题描述】:
我有一个 jquery 脚本,可以为某个州的县创建图像翻转。该脚本在 XML 文档中查询县 x/y 坐标和信息,然后在将鼠标悬停在州图像上的县上时显示此信息。此外,将显示随机县和县信息,直到悬停其中一个县;一旦观众将鼠标悬停在县城外,随机显示就会恢复。
该脚本在 FireFox 和 Chrome 中运行良好,但在 Internet Explorer 7 - 8 中不适用。在 IE 中,随机显示有效,但大约 75% 的县不会响应悬停功能,显然没有模式为什么它们不响应(正如我所说,它们在其他浏览器中运行良好)。
//____________________________________________________________________________________________________________ CONSTANTS
var DATA_SOURCE = '/CountyData.js'; // URL of the page that serves JSON county data
var IMAGE_PATH = '/images/counties/'; // Relative path to the county image folder
var CSS_WRAPPER_CLASS = 'countyWrapper'; // CSS class to apply to each county's wrapper <div>
var CSS_IMAGE_CLASS = 'countyImage'; // CSS class to apply to <img> tags
var CSS_INFO_CLASS = 'countyInfo'; // CSS class to apply to info <div> tags
var ROTATION_INTERVAL = 10; // seconds to show each random county while rotating
//____________________________________________________________________________________________________ PRIVATE VARIABLES
var _isHovering = false;
var _autoSelectedCounty = null;
//______________________________________________________________________________________________________ FUN BEGINS HERE
function highlightRandomCounty() {
// only show a new county if we're not hovering over another one
if (!_isHovering) {
var children = $('#map-holder').children('div');
var randomIndex = parseInt(Math.random() * children.length);
_autoSelectedCounty = $(children[randomIndex]).children('img')[0];
hideAllCounties();
showCounty(_autoSelectedCounty);
}
}
function showCounty(countyImage) {
$(countyImage).stop().animate({ opacity: 1.0 }, 500);
$(countyImage).siblings().animate({ opacity: 1.0 }, 500);
}
function hideCounty(countyImage) {
$(countyImage).stop().animate({ opacity: 0.0 }, 0);
$(countyImage).siblings().animate({ opacity: 0.0 }, 0);
}
function hideAllCounties() {
var counties = $('#map-holder').children('div');
for (var i = 0; i < counties.length; i++) {
hideCounty($(counties[i]).children('img')[0]);
}
}
$(document).ready(function() {
// show the pre-loader
var preloader = document.createElement('div');
$('#map-holder').append(preloader);
$(preloader).attr('id', 'preloader');
//testing var
var countyCount = 1;
$.getJSON(DATA_SOURCE, function(data) {
$.each(data.countyData, function(i, item) {
// create the container <div> and append it to the page
var container = document.createElement('div');
$('#map-holder').append(container);
// configure the container
$(container).attr('id', 'div' + item.data_county);
$(container).attr('class', CSS_WRAPPER_CLASS);
$(container).attr('style', 'left:'+ item.data_x + 'px; top:' + item.data_y + 'px;');
// create the <img> and append it to the container
var countyImage = document.createElement('img');
$(container).append(countyImage);
// configure the image
$(countyImage).attr('id', item.data_county + 'Image');
$(countyImage).attr('class', CSS_IMAGE_CLASS);
$(countyImage).attr('src', IMAGE_PATH + item.data_county_image + '.png');
$(countyImage).load(function() {
// wait for the image loads before setting these properties
$(this).attr('width', countyImage.width);
$(this).attr('height', countyImage.height);
});
$(countyImage).hover(function() {
_isHovering = true;
hideCounty(_autoSelectedCounty);
showCounty($(this));
},
function() {
_isHovering = false;
hideCounty($(this));
});
$(countyImage).click(function() {
window.location.href = item.factsLink;
});
// create the info <div> and append it to the container
var countyInfo = document.createElement('div');
$(container).append(countyInfo);
$(countyInfo).attr('id', item.data_county + 'Info');
$(countyInfo).attr('class', CSS_INFO_CLASS);
// Handle county naming exceptions
var countyName = item.data_county.toUpperCase();
if (countyName == 'SAINTJOYVILLE') {
countyName = 'ST. JOYVILLE';
} else {
if (countyName.indexOf("SAINT") > -1) {
countyName = "ST. " + countyName.substr(5);
}
countyName += " COUNTY";
}
if (item.story_link == 'no') {
$(countyInfo).html('<strong>' + countyName + '</strong><br />' + item.data_total_students + ' Students<br />' + item.data_total_alumni + ' Alumni<br />' + '$' + item.economicImpact + ' Impact<br />Click For More...');
} else {
$(countyInfo).html('<strong>' + countyName + '</strong><br />' + item.data_total_students + ' Students<br />' + item.data_total_alumni + ' Alumni<br />' + '$' + item.economicImpact + 'Impact<br /><strong>Latest story:</strong><br /><img src="' + item.story_link + '" alt="story" height="75" width="110"><br />' + item.story_title + '<br />Click For More...');
}
});
});
// hide the pre-loader
$('#preloader').animate({ opacity: 0.0 }, 1000);
//randomly rotate through the counties
setInterval(highlightRandomCounty, ROTATION_INTERVAL * 1000);
});
是否需要启用/禁用某些功能才能在 IE 中正常运行?也许是 IE 中的某种 .getJSON/缓存问题?非常感谢任何信息。
【问题讨论】:
-
来自已知问题列表 "Internet Explorer 将相对 URL 转换为绝对 URL。不幸的是,对于这个问题没有合理的解决方法。 " 也许这与它有关?
-
我正在使用 HTML 和 Jquery,以及 XML 文件。
-
谢谢大家的建议。到目前为止,似乎没有什么可以解决这个问题。我将所有代码的副本上传到另一个网络主机,它在 IE 中运行良好。那么,我当前的网络主机中一定有一些东西以 IE 不喜欢的方式影响 JSON/Jquery?我仍在为此拉扯头发,因此非常感谢任何其他建议。提前致谢!
-
Thantos - 我在下面更新了我的答案,如果有帮助请告诉我。
标签: jquery internet-explorer getjson