我是Javascript新手,需要大學課程上的一些幫助以用字符串「spaces」替換字符串中的所有空格。javascript - 用字符串替換空格
我用下面的代碼,但我不能得到它的工作:
<html>
<body>
<script type ="text/javascript">
// Program to replace any spaces in a string of text with the word "spaces".
var str = "Visit Micro soft!";
var result = "";
For (var index = 0; index < str.length ; index = index + 1)
{
if (str.charAt(index)= " ")
{
result = result + "space";
}
else
{
result = result + (str.charAt(index));
}
}
document.write(" The answer is " + result);
</script>
</body>
</html>
你如果語句需要==而不是= =。 ==用於比較,=用於將變量保存在變量中(賦值) –
它以什麼方式「不起作用」? – graphicdivine