Javascript:错误无法设置属性' ...'未定义的

时间:2016-11-18 21:39:48

标签: javascript

为什么这行JS代码返回错误"无法设置属性...未定义"?

String nt = df.format(dates);

我正确引用了div元素的id。

document.forms["myForm"]["english_error"].textContent = "English sentence is required";

1 个答案:

答案 0 :(得分:1)

仅映射表单控件,基本上只映射<input><textarea><select>标记:

var sentence = document.forms["myForm"]["english"];

<input>标记是否有效:

document.forms["myForm"]["english_error"].textContent = "English sentence is required";

english_error是正常的<div>因此不起作用,您必须使用:

document.getElementById("english_error").textContent = "English sentence is required";

还有班级名称:

document.getElementsByClassName("val_error")[0].textContent = "English sentence is required";