for...in 循环在 Chrome 中工作,但在 IE 中不工作?
for...in looping working in Chrome, but not in IE?
好的,问题来了...
这是我正在编写的文件的简化版本...
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>_</title>
<script src="/sifu/query/jquery.js"></script>
<script>
/********************************************************/
str='abcdefghijklmnopqrstuvwxyz'
/********************************************************/
function pop(){
/****************************/
sx='for(ia in str)'+'\n';
for(ia in str)
sx+='\n'+ia+' => '+str[ia];
enX.innerText=sx;
/****************************/
sy='for(ib=0;ib<str.length;ib++)'+'\n';
for(ib=0;ib<str.length;ib++)
sy+='\n'+ib+' => '+str[ib];
enY.innerText=sy;
/****************************/
}
/********************************************************/
$(document).ready(function(){
pop()
})
/********************************************************/
</script>
</head>
<body>
<table style="width:100%;text-align:center">
<tr>
<td id="enX">enX</td>
<td id="enY">enY</td>
</tr>
</table>
</body>
</html>
演示
str = 'abcdefghijklmnopqrstuvwxyz'
function pop() {
sx = 'for(ia in str)' + '\n';
for (ia in str) {
sx += '\n' + ia + ' => ' + str[ia];
}
enX.innerText = sx;
sy = 'for(ib=0;ib<str.length;ib++)' + '\n';
for (ib = 0; ib < str.length; ib++) {
sy += '\n' + ib + ' => ' + str[ib];
}
enY.innerText = sy;
}
$(document).ready(function() {
pop()
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%;text-align:center">
<tr>
<td id="enX">enX</td>
<td id="enY">enY</td>
</tr>
</table>
当我在 chrome 中加载它时它工作正常...我得到两个相等的列 0-25|a-z.
然而,当我在 IE 中加载它时,
Column X, only has the header for(ia in str)
and contains no instances of the loop ie: 0 => a
etc...
和
Column Y, contains both the header for(ib=0;ib<str.length;ib++)
and 26 instances of # => undefined
, where #
is the loop number.
我很抱歉提出这样一个 简单 的问题,但我最近一直在自学如何编写专门针对 [=67 的网页=],将 IE 抛在脑后,似乎在花时间学习如何在 chrome[=58 中编写代码=] 我忘记了 IE...
中编码的基础知识
欢迎所有指点...请解释我哪里出错了
我找不到参考,但我很确定某些版本的 IE 不支持索引字符串,您应该使用 charAt
函数。这是真的,在那些版本的 IE 中,for .. in
没有什么可以迭代的字符串。
通过使用 .split('')
将字符串转换为数组并使用 for .. in
代码应该在所有浏览器中工作。
好的,问题来了...
这是我正在编写的文件的简化版本...
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>_</title>
<script src="/sifu/query/jquery.js"></script>
<script>
/********************************************************/
str='abcdefghijklmnopqrstuvwxyz'
/********************************************************/
function pop(){
/****************************/
sx='for(ia in str)'+'\n';
for(ia in str)
sx+='\n'+ia+' => '+str[ia];
enX.innerText=sx;
/****************************/
sy='for(ib=0;ib<str.length;ib++)'+'\n';
for(ib=0;ib<str.length;ib++)
sy+='\n'+ib+' => '+str[ib];
enY.innerText=sy;
/****************************/
}
/********************************************************/
$(document).ready(function(){
pop()
})
/********************************************************/
</script>
</head>
<body>
<table style="width:100%;text-align:center">
<tr>
<td id="enX">enX</td>
<td id="enY">enY</td>
</tr>
</table>
</body>
</html>
演示
str = 'abcdefghijklmnopqrstuvwxyz'
function pop() {
sx = 'for(ia in str)' + '\n';
for (ia in str) {
sx += '\n' + ia + ' => ' + str[ia];
}
enX.innerText = sx;
sy = 'for(ib=0;ib<str.length;ib++)' + '\n';
for (ib = 0; ib < str.length; ib++) {
sy += '\n' + ib + ' => ' + str[ib];
}
enY.innerText = sy;
}
$(document).ready(function() {
pop()
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%;text-align:center">
<tr>
<td id="enX">enX</td>
<td id="enY">enY</td>
</tr>
</table>
当我在 chrome 中加载它时它工作正常...我得到两个相等的列 0-25|a-z.
然而,当我在 IE 中加载它时,
Column X, only has the header
for(ia in str)
and contains no instances of the loop ie:0 => a
etc...
和
Column Y, contains both the header
for(ib=0;ib<str.length;ib++)
and 26 instances of# => undefined
, where#
is the loop number.
我很抱歉提出这样一个 简单 的问题,但我最近一直在自学如何编写专门针对 [=67 的网页=],将 IE 抛在脑后,似乎在花时间学习如何在 chrome[=58 中编写代码=] 我忘记了 IE...
中编码的基础知识欢迎所有指点...请解释我哪里出错了
我找不到参考,但我很确定某些版本的 IE 不支持索引字符串,您应该使用 charAt
函数。这是真的,在那些版本的 IE 中,for .. in
没有什么可以迭代的字符串。
通过使用 .split('')
将字符串转换为数组并使用 for .. in
代码应该在所有浏览器中工作。