GetElementById not finding ID
GetElementById not finding ID
嗨,我一直在研究这段代码,它为每个单词创建一个带有单选按钮的表格。目前,这工作正常,但是我没有在单选按钮上单击功能并提交。它们都会在单击时进入正在调用的函数,但是一旦进入函数并且我调用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 的替代方案是什么?
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- Href:当前DIV中的目标ID
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- Javascript复选框函数:;缺少:在属性id之后"
- Twitter引导程序Typeahead-Id&标签
- 如何与特定ID交谈
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- 如何在速度模板中获取LiferayPortlet实例id
- 谷歌地图可以't添加正确的标记ID
- GetElementById not finding ID