gpt4 book ai didi

javascript - IE 全局变量 arrayName.className 为 null

转载 作者:行者123 更新时间:2023-11-28 09:38:26 24 4
gpt4 key购买 nike

我有一个 jQuery ajax 调用来填充一个数组;该数组应该可以在另一个函数中访问。它实际上存在于 FireFox 和 Safari 中,但不在 IE 中。IE 说: SCRIPT5007 无法获取属性“名称”的值:对象为 null 或未定义

看起来“globalDataArray[i].name”和“globalDataArray[i].objectid”都有问题。两者都被 FF 和 IE 完美评估和使用,所以并不是空的。有人对为什么会发生这种情况有任何想法吗?我用谷歌搜索了很多;在末尾使用逗号或其他内容的常见问题并不是解决方案。

这里设置了变量:

var globalDataArray = [];
function retrieveContentData(content){


$.ajax({
url: 'http://services.arcgis.com/nSZVuSZjHpEZZbRo/ArcGIS/rest/services/NLCito/FeatureServer/0/query',
data: {
where: content,
geometryType: 'esriGeometryEnvelope',
spatialRel: 'esriSpatialRelIntersects',
outFields: '*',
returnGeometry: false,
returnIdsOnly: false,
returnCountOnly: false,
f: 'pjson'
},
success: function(data){
data = $.parseJSON(data);//Always parse JSON data
var features = data.features;
for (var i=0; i<features.length; i++) {
//globalDataArray.push(features[i].attributes.NAME);
//globalDataArray[features[i].attributes.OBJECTID] = features[i].attributes.NAME;
globalDataArray[i] = { "objectid": features[i].attributes.OBJECTID,
"name": features[i].attributes.NAME,
"type": features[i].attributes.Type
};
}
shuffle(globalDataArray);//Shuffle the array items

//Count total and set progress report
$('#totalTasks').text(features.length);

//Initialize the progress bar and create the first task
updateProgressBar(0);
createNewTask();

}//End success
});//End Ajax call

}//End function

这是我想再次使用它的地方:

function validateAnswer(){
//Prevent validating if task div not shown
if($('#task').is(":visible")){

var passedTask = false;

var typedAnswer = $('#taskAnswerInput').val();

var desiredAnswer = globalDataArray[i].name;
var desiredAnswerShort = desiredAnswer.replace(/\(.*?\)/, "");//Remove eveything within and with bracklets
desiredAnswerShort = jQuery.trim(desiredAnswerShort);//Remove any whitespace on beginning and end of the string

if(typedAnswer === desiredAnswer || typedAnswer === desiredAnswerShort){
alert('Exact, helemaal goed!');
$('#tasksRight').text(parseInt($('#tasksRight').text()) +1);
passedTask = true;
updateProgressBar(i);
}else{
alert('Jammer, dat is niet het goede antwoord');
}

if(passedTask == true){
nextTask();
}
}//end if visible
}

这是调用 createNewTask() 函数的地方:

var i = 0;
function createNewTask(){
//Since a new tasks is started, let's update the progress
$('#tasksDone').text(i);

//Highlight a single place
executeQuery(globalDataArray[i].objectid);

//Change tasks text
var type = globalDataArray[i].type;
if(type === 'Plaats'){ type = 'Welke plaats';}
if(type === 'Gebied'){ type = 'Welk gebied';}
if(type === 'Water'){ type = 'Welk water';}
if(type === 'Provincie'){ type = 'Welke provincie';}
$('#taskPointType').html(type);

$('#taskAnswerInput').val('');//Clear the input field
}

function giveupTask(){
var correctAnswer = globalDataArray[i].name;
alert(correctAnswer);
$('#tasksWrong').text(parseInt($('#tasksWrong').text()) +1);//Update currentWrong
nextTask();
}

//Aparte functie, om validateAnswer() flexibeler te houden
function nextTask(){
//fire new task
i++;
if(i < globalDataArray.length){
//Update progressbar
updateProgressBar(i+1);//+1 since i starts with 0
createNewTask();
}else{
//All tasks done
alert('Einde, alle plaatsen gehad');
}
}

最佳答案

解决了。对于任何有兴趣的人:

看起来 IE 并不总是返回正确的错误、jquery ajax 成功、完整和错误函数,我们可以帮助修复它。最后发现问题出在跨域ajax调用上; IE 会阻止它们,除非您使用 dataType: jsonp。仅常规 json 还不够好。

感谢您分享您的想法!

关于javascript - IE 全局变量 arrayName.className 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12689160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com