[英]Uncaught TypeError: undefined is not a function - not JQuery
I feel terrible for submitting another Uncaught TypeError: undefined is not a function question. 我提交另一个Uncaught TypeError感到很糟糕:undefined不是函数问题。 But after reading about 20 different answers to these types of questions, either I am missing the point, or the problem was different in those cases.
但是,在阅读了关于这些类型问题的大约20种不同答案之后,我还是没听清重点,或者在这些情况下问题有所不同。
I have a form with two radio buttons. 我有一个带有两个单选按钮的表单。 When one is clicked I want it to change the innerHTML of a
<tr></tr>
tag. 当我单击它时,我希望它更改
<tr></tr>
标记的innerHTML。
Here is my JFiddle . 这是我的JFiddle 。
The HTML: HTML:
<form name="myForm" autocomplete="off" action="index.php" method="post" enctype="multipart/form-data">
<label for="mode">Text</label>
<input type="radio" name="mode" value="text" id="setText" />
<label for="mode">Image</label>
<input type="radio" name="mode" value="img" id="setImg" />
<table>
<tr id="headerInput"></tr>
</table>
</form>
The Javascript: Javascript:
document.getElementById("setText").addEventListener("click", function () {
window.getElementById("headerInput").innerHTML = '<td>Title</td><td><input type="text" name="title" value="Hello" /></td>';
});
document.getElementById("setImg").addEventListener("click", function () {
window.getElementById("headerInput").innerHTML = '<td>Banner</td><td><input type="file" name="banner" /></td>';
});
I have tried a few different versions of the same thing, like creating named functions, using the onclick="someFunction()"
attribute and object.onclick
as well. 我已经尝试了几种不同版本的同一件事,如创建命名的功能,使用
onclick="someFunction()"
属性, object.onclick
为好。 All of them give me the same error. 他们都给了我同样的错误。 Your help to understand what I am doing is greatly appreciated.
非常感谢您对我的理解。
window.getElementById()
isn't defined normally in a browser. 在浏览器中通常未定义
window.getElementById()
。
Looking up property 'getElementById'
on window
returns undefined
. 在
window
上查找属性'getElementById'
返回undefined
。 You're then attempting to invoke undefined
, hence Uncaught TypeError: undefined is not a function . 然后,您尝试调用
undefined
,因此Uncaught TypeError:undefined不是一个function 。
You probably mean to use document
there instead. 您可能打算在那儿使用
document
。
You should refer document.getElementById() rather than window.getElementById(). 您应该引用document.getElementById()而不是window.getElementById()。 Also, changing the innerHTML is fine but afaik we should refer to the td inside the tr for any manipulations.
另外,更改innerHTML很好,但是,对于任何操作,我们都应参考tr内部的td。 That's a best practice.
这是最佳做法。
document.getElementById("headerInput").innerHTML
Working fiddle: http://jsfiddle.net/7fpBg/ 工作提琴: http : //jsfiddle.net/7fpBg/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.