javascript - 为什么我的 charAt 函数不能在我的 javascript 上运行?

标签 javascript html

我编写了一个简单的函数,每次更新一个文本区域时都会执行该函数,但我想获取该文本区域中文本的最后一个字符。
为此,我使用了 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/

相关文章:

html - 更改单选文本,同时将输入和跨度保留在标签标记中

javascript - 转到 Javascript 中的随机页面

javascript - 着色单元格谷歌图表 - 散点图

html - CSS网格悬停在区域之外

javascript - 如何以编程方式单击从另一个页面加载的按钮?

javascript - 无法让第二个 javascript 函数工作?

html - dia 导出为 html

javascript - Firebase:如何强制数据库按输入顺序存储值?

javascript - 一段时间后 Canvas 开始滞后

javascript - pattern 属性仅在页面重置后通过