我编写了一个简单的函数,每次更新一个文本区域时都会执行该函数,但我想获取该文本区域中文本的最后一个字符。
为此,我使用了 charAt
,但随后代码停止工作。
请任何人告诉我我犯了什么错误。
// array is goes here
function startText() {
var str=document.getElementById("intxt").value;
var matchkeyword = str.charAt(str.length-1);
// code extends
var sugest = "";
for (var i = 0; i < consonants.length; i++) {
if (consonants[i].match(matchkeyword.value) != null && matchkeyword.value != "") {
sugest = sugest + "<br>" + consonants[i] + "=" + consonantsUni[i];
}
}
y = document.getElementById("sugdiv");
y.innerHTML = sugest;
}
最佳答案
将其分配给这样的变量后
var matchkeyword = str.charAt(str.length-1);
您不应在 matchkeyword
上使用 value
。而不是,
if(consonants[i].match(matchkeyword.value) != null && matchkeyword.value != "") {
使用
if(consonants[i].match(matchkeyword) != null && matchkeyword != "") {
注意:如果您使用的是 Chrome,每当脚本“停止工作”时,请按 F12,这将打开开发人员工具。您将能够从中得到线索。
关于javascript - 为什么我的 charAt 函数不能在我的 javascript 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19636893/