Javascript 无法给变量赋值并导致中断 --> word5 = (关键字[n].长度);

发布于 2024-12-04 04:38:08 字数 1866 浏览 0 评论 0 原文

做了一些javascript自学,程序本身很简单。它在输入中搜索关键字,当找到关键字时,它将索引值放入列表占位符中。 (我将开发稍后的代码来删除重复)。

然而,有一个障碍。我正在尝试做一些非常简单的事情。我所做的就是将变量words5 分配给keywords[n].length。现在我发现 keywords[n].length 嵌套到 document.write() 中时确实会打印出正确的值。我还发现 keywords[n] 嵌套到 document.write() 中时也会打印正确的值。但如果在 document.write 之外的代码中使用其中任何一个,则会导致代码中断。

已经研究了几个小时了。而且我一直没能找到解决办法。你们有什么线索吗?我用来测试它的代码已被注释掉。

<html>
<body>

<script type="text/javascript">


function separate()
{
    contents = document.myForm.event.value;
    placeholders = [32342423, 253234523];
    keywords = [" in ", " at ", " on ", " for "];

    for(n=0;n<=keywords.length;n++)
    {
        for (i=0;i<=contents.length;i++)
        {
            //document.write(i,"+", i+keywords[n].length, keywords[n])
            //document.write(keywords[n].length)
            word5 = (keywords[n].length);
            //document.write(" " + contents.slice(i,i+word5) + " ")
            //if (contents.slice(i,i+4) == " at ")
            //if (contents.slice(i,i+wordlength) == " at ")
            //if (contents.slice(i,i+4) == keywords[n])
            if (contents.slice(i,i+word5) == keywords[n])
            {
                placeholders.push(i);
            }
        }
    }
    document.getElementById("sliced").innerHTML = placeholders;
    printer();
}


function printer()
{
    contents = document.myForm.event.value;
    document.getElementById("MSG").innerHTML = contents;
}
</script>



<form name="myForm" method="post">
Add Event: <input type="text" name="event" value="Whatever at hello at crazy" /><br />
<input type=button value="Submit" onClick="separate()" />
</form>

<SPAN ID="sliced">&nbsp;</SPAN>
<p></p>
<SPAN ID="MSG">&nbsp;</SPAN>



</body>
</html>

非常感谢任何帮助。真是救星=)

Doing some javascript self-learning and the program itself is very simple. It searches through the input for keywords and when it finds it, it places the index values into list placeholders. (I'll develop later code to remove repetitions).

However, a snag. I'm trying to do something very simple. All I'm doing is assigning the variable words5 to keywords[n].length. Now I've discovered the keywords[n].length does print me the correct value when nested into document.write(). I've also discovered that keywords[n] also prints the correct value when nested into document.write(). BUT if either are used in the code outside of a document.write, it causes the code to break.

Been going at it for hours. And I haven't been able to find a solution. Do you guys have any clues? The code that I used to test it out is commented out.

<html>
<body>

<script type="text/javascript">


function separate()
{
    contents = document.myForm.event.value;
    placeholders = [32342423, 253234523];
    keywords = [" in ", " at ", " on ", " for "];

    for(n=0;n<=keywords.length;n++)
    {
        for (i=0;i<=contents.length;i++)
        {
            //document.write(i,"+", i+keywords[n].length, keywords[n])
            //document.write(keywords[n].length)
            word5 = (keywords[n].length);
            //document.write(" " + contents.slice(i,i+word5) + " ")
            //if (contents.slice(i,i+4) == " at ")
            //if (contents.slice(i,i+wordlength) == " at ")
            //if (contents.slice(i,i+4) == keywords[n])
            if (contents.slice(i,i+word5) == keywords[n])
            {
                placeholders.push(i);
            }
        }
    }
    document.getElementById("sliced").innerHTML = placeholders;
    printer();
}


function printer()
{
    contents = document.myForm.event.value;
    document.getElementById("MSG").innerHTML = contents;
}
</script>



<form name="myForm" method="post">
Add Event: <input type="text" name="event" value="Whatever at hello at crazy" /><br />
<input type=button value="Submit" onClick="separate()" />
</form>

<SPAN ID="sliced"> </SPAN>
<p></p>
<SPAN ID="MSG"> </SPAN>



</body>
</html>

Any help is greatly appreciated. Saviours really =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-12-11 04:38:08

当我运行您的代码时出现的错误是:

wordlength is undefined

这导致代码失败。你在哪里定义字长

更新

另请注意,当您循环遍历数组时,数组的索引为零。也就是说,数组中的第一项是第 0 项。数组中的项数计数始终大于索引之和(索引总和 + 1)。

这基本上意味着,当您在 for 循环中使用 <= (小于或等于)运算符时,您将从零循环到集合中的项目数,但是,因为您的数组索引开始为零,您最终将从数组中请求一个不存在的项目。

为了确保仅循环可用的项目,请使用 < 运算符而不是 <=

The eror I get when I run your code is that:

wordlength is undefined

This is causing the code to fail. Where are you defining wordlength?

Update

Also be aware that when you're looping through arrays that arrays have a zero index. That is, the first item in the array is item 0. The count of the number of items in the array is always going to be greater than the sum of the indexes (index sum + 1).

This basically means that when you use the <= (less than or equel to) operator in your for loop, you're looping from zero to the number of items in your collection, however, since your array index begins are zero, you're eventually going to be requesting an item from your array that doesn't exist.

To ensure that you only loop over the available items use the < operator rather than <=.

我为君王 2024-12-11 04:38:08

这不是破坏您的代码的原因,似乎您没有在代码的任何地方定义变量“wordlength”,这就是破坏您的代码的原因。

word5 = (keywords[n].length) 实际上有效。

另外我建议为 Firefox 获取“firebug”插件,这是调试任何 javascipt 或 html 代码的最佳方式,使用控制台使用“console.log(myvar)”而不是 document.write 输出调试语句,这是一个漂亮的老式的做事方式。

如果这个解决方案不适合您,请告诉我:)

编辑:
我做了另一组测试,发现原因是什么,你的第一个循环运行的数组数量比你当前的数组数量多 1,所以不要让它“也小于或等于”,它应该小于:

< code>for(n=0;n

因此,为什么你得到 keywords[n] 是未定义的,因为数组中没有 keywords[4]

that's not what's breaking your code, it seems like you are not defining the variable "wordlength" anywhere on your code, this is what's breaking your code.

word5 = (keywords[n].length) actually works.

Also I would recommend getting "firebug" addon for firefox, it's the best way to debug any javascipt or html code, use the console to output your debug statements using "console.log(myvar)" instead of document.write which is a pretty old fashioned way of doing things.

Let me know if this solution doesn't work for you :)

EDIT:
I did another set of tests and found out what the reason was, your first loop is running 1 more than what your current number of arrays are, so instead of making it "less than or equal too" it should just be less than:

for(n=0;n<keywords.length;n++)

Hence why you're getting a keywords[n] is undefined, as there is no keywords[4] in the array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文