GetElementById not finding ID

GetElementById not finding ID

本文关键字:ID finding not GetElementById      更新时间:2023-09-26

嗨,我一直在研究这段代码,它为每个单词创建一个带有单选按钮的表格。目前,这工作正常,但是我没有在单选按钮上单击功能并提交。它们都会在单击时进入正在调用的函数,但是一旦进入函数并且我调用getElementById来验证或更改颜色,则找不到 ID,或者至少这是我假设的,因为在发出警报后,什么都没有出现。

我对javascript相对较新,所以任何建议都会有所帮助。有人告诉我,这可能是因为它们在创建之前被调用,但从我在代码中看到的情况来看,情况并非如此。有什么建议吗?

编辑:

这是出现问题的代码。只有西班牙人曾经点击它。没有进行提交验证,因为我认为它们是相同的问题

for (i = 0; i < sentences.length; i++) {
    document.write('<p><a onclick ="javascript:ShowHide(''Sentence' + (i + 1) + ''')" href="javascript:;"><font color="#0000FF"><u>Sentence' + (i + 1) + '</u></font></a></p>');
    document.write('<table style="display: none; table-layout: fixed" id="Sentence' + (i + 1) + '" border = "1">');
    writeSentence(i, words, "Sentences");
    document.write('</table>');
    document.write('<input type="checkbox" name="Sentence_lang ' + (i + 1) + '" id = "Sentence_lang ' + (i + 1) + '" value="All_English" /> All English    ');
    document.write('<input type="checkbox" name="Sentence_lang ' + (i + 1) + '" id = "Sentence_lang ' + (i + 1) + '" value="All_Spanish" /> All Spanish <br />');
}
document.write('</p>')
function writeSentence(i, array, name) {
    var Name = name;
    document.write('<tr>');
    document.write('<td>');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        var word = array[i][j];
        document.write('<td>');
        if (Name == "Sentences") document.write('<span class="kept" id="word_' + i + '_' + j + '">');
        document.write(word);
        if (Name == "Sentences") document.write('</span> ');
        document.write('</td>');
    }
    document.write('</tr>');
    document.write('<td>');
    document.write('English');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        document.write('<td>');
        document.write('<input type="radio" name="' + Name + (i + 1) + '_' + (j) + '" id="Eng_' + (i + 1) + '_' + (j) + '" value="English" >');
        document.write('</td>');
    }
    document.write('</tr>');
    document.write('<tr>');
    document.write('<td>');
    document.write('Spanish');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        document.write('<td>');
        document.write('<input type="radio" onclick = "color(' + i + ',' + j')" name="' + Name + (i + 1) + '_' + (j) + '" id="Spa_' + (i + 1) + '_' + (j) + '" value="Spanish"> ');
        document.write('</td>');
    }
    document.write('</tr>');
    document.write('<tr>');
    document.write('<td>');
    document.write('Other');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        document.write('<td>');
        document.write('<input type="radio"  name="' + Name + (i + 1) + '_' + (j) + '" id="Other_' + (i + 1) + '_' + (j) + '" value="other" > ');
        document.write('</td>');
    }
    document.write('</tr>');
    if (Name == "Sentences") for (j = 0; j < array[i].length; j++)
    document.write('<input type="radio" input style= "display: none" name="' + Name + (i + 1) + '_' + (j) + '" id="' + Name + (i + 1) + '_' + (j) + '" value="Norm" checked>');
}
function color(i, j,lang) {
    var word = document.getElementById('word_' + i + '_' + j + '');
    aleart(word);
    if (lang == "Spa") word.className = "blue";
}

编辑最后一个 if 语句是我正在尝试做的。第三个参数目前为空,因为我无法弄清楚如何正确传递字符串 Spa。但如果需要,我可以绕过它。

您有一个拼写错误:aleart(word); ,该错误应该在您的控制台中可见。将其更改为alert(word);,应出现警告框。

除此之外,您不应该使用 document.write .加载文档后,它将不起作用。请参阅 document.write 的替代方案或 document.write 的替代方案是什么?