按下HTML图表按钮时,它会激活createChart()方法。但是,如果textarea中的值不超过25个字符,则网页会冻结。此外,只有字母" a"正在引用该方法。
<script type = "text/javascript">
orangeBar = "./orange.gif";
function createChart()
{
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
textValue = document.getElementById("text").value;
max = 0;
for (i=0;i<alphabet.length;i++)
{
value = numberOf(alphabet[i], textValue);
if (value > max)
{
max = value;
}
}
document.write(max);
}
function numberOf(letter, string)
{
count = 0;
for (i=0; i<string.length; i++)
{
if (string[i].toLowerCase() == letter)
{
count++;
}
}
return count;
}
</script>
谢谢。我对编码很陌生。我可以回答任何问题。
答案 0 :(得分:2)
使用for (i=0;i<alphabet.length;i++)
时,i
成为全局变量。然后,您在i
中使用相同的for (i=0; i<string.length; i++)
,它会更改i
函数的createChart()
。将var
放在i
之前,使其成为本地。
for (var i=0;i<alphabet.length;i++)
和
for (var i=0; i<string.length; i++)