0 javascript internet-explorer
我for each()在这个函数中有一个循环,通过搜索互联网我知道,每个循环在 IE 中都不起作用。为了节省我的时间,我简单地try {} catch {}在它周围加上了一个:,但 IE 仍然通过函数调用提醒我有一个错误。
为什么 IE 11 有效?代码:
代码:
function classAndIdSpy() {
var req = new XMLHttpRequest();
var x = document.getElementsByClassName("spy");
var page_you_on = window.location.href.split("/");
var json56 = '{ "div_ids_classes" : [{"PAGE":"'+page_you_on[page_you_on.length-1]+'"},';
try {
// Block of code to try.
for(var xy of x) {
json56 += '{"SPY_ID":"'+xy.id+'","CLASS":"'+xy.parentNode.className+'","ID":"'+xy.parentNode.id +'"}';
json56 += (x.length-1 > [].indexOf.call(x, xy)) ? ',' : '';
}
json56 += ']}';
req.open('POST', '../admin/id_class_colect.php', true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send("json2="+json56);
var status;
req.onreadystatechange = function() {//Call a function when the state changes.
if(req.readyState == 4 && req.status == 200) {
status = req.responseText;
console.log("Send ajax. Colected Id and Class information of content DIVs into database. Status:"+status);
}}
}
catch(err) {
console.log("Your browser doesn't support ID and Class information of content DIV into database. Please use another browser.");
return false;
}
} // Endo of ClassAndIDspy
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到以下错误:SCRIPT1004:预期的 ';' (该函数在 FF 中运行良好,我认为没有缺少分号)
而且,这是我希望 Internet Explorer 忽略的部分: 代码:
for(var xy of x) {
json56 += '{"SPY_ID":"'+xy.id+'","CLASS":"'+xy.parentNode.className+'","ID":"'+xy.parentNode.id +'"}';
json56 += (x.length-1 > [].indexOf.call(x, xy)) ? ',' : '';
}
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助 !
你似乎误解了try- 的意思catch。
try和catch用于处理你的JavaScript代码运行时出现异常。您的代码无法在 IE 中运行,因为它无法编译。没有什么try和catch可以做的编译错误。
在我看来,您的选择是:
for循环而不是for (xy of x)循环。这样做并不需要太多的努力,正如我在评论中指出的那样,您的for循环无论如何都在使用数组中元素的索引。我强烈建议您放弃让 IE 忽略代码块的想法。
| 归档时间: |
|
| 查看次数: |
4893 次 |
| 最近记录: |