简体   繁体   English

未捕获的TypeError:undefined不是函数-不是JQuery

[英]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.

相关问题 jQuery的,遗漏的类型错误:未定义是不是一个函数 - Jquery, Uncaught TypeError: undefined is not a function 未捕获的TypeError:undefined不是jQuery的函数 - Uncaught TypeError: undefined is not a function with jQuery 未捕获的TypeError:undefined不是函数jquery - Uncaught TypeError: undefined is not a function jquery 未捕获的TypeError:undefined不是函数(jQuery和jQuery Easing) - Uncaught TypeError: undefined is not a function (jQuery & jQuery Easing) 未捕获的TypeError:未定义不是我的jQuery脚本中的函数 - Uncaught TypeError: undefined is not a function in my jQuery script 未捕获的TypeError:undefined不是函数-使用Jquery标签 - Uncaught TypeError: undefined is not a function - Using Jquery Tabs Uncaught TypeError:未定义不是函数jQuery自动完成 - Uncaught TypeError: undefined is not a function jquery autocomplete jQuery .on()引发Uncaught TypeError:undefined不是一个函数 - jQuery .on() throws Uncaught TypeError: undefined is not a function jQuery sortable issue = Uncaught TypeError:undefined不是函数 - jQuery sortable issue = Uncaught TypeError: undefined is not a function jQuery滑块未捕获的TypeError:undefined不是函数 - jquery slider Uncaught TypeError: undefined is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM