Javascript 类型错误:对象不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5959045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: object is not a function
提问by remuladgryta
I'm developing this webapp for my school. The page is supposed to filter entries by the url parameter "class". This works fine as far as i can tell, but when i try to change the filter it gives "TypeError: object is not a function". What am i doing wrong?
我正在为我的学校开发这个网络应用程序。该页面应该通过 url 参数“class”过滤条目。据我所知,这很好用,但是当我尝试更改过滤器时,它给出了“TypeError: object is not a function”。我究竟做错了什么?
<html>
<head>
<TITLE>Cancelled lessons</TITLE>
</head>
<body>
<script>
function filter(text){
text = text.toLowerCase();
for (i=0;i<lessonList.length;i++){
if(lessonList[i].innerHTML.toLowerCase().indexOf(text)==-1){
lessonList[i].style.display = "none";
}
else{
lessonList[i].style.display ="";
}
}
}
function gup( name )
{
name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
</script>
<form>
Filter: <input type="text" id="filter" oninput="filter(document.getElementById('filter'))"/>
</form>
<div id="lessons">
<div class="entry"> MaA 11:00 C131 Ej NV3C</div>
</div>
<script>
var lessonList = document.getElementsByClassName("entry");
var filterField =document.getElementById("filter");
filterField.value = gup("class");
filter(filterField.value);
</script>
</body>
</html>
采纳答案by Tom Chandler
It looks like the "oninput" handler calls the filter function from the scope of the form (document.forms[0]) rather than globally. If you check the value of document.forms[0].filter it'll return the input tag. You just need to make sure that the function name is different than the input name/id.
看起来“oninput”处理程序从表单的范围(document.forms[0])而不是全局调用过滤器函数。如果您检查 document.forms[0].filter 的值,它将返回输入标签。您只需要确保函数名称与输入名称/ID 不同。
This also means you don't need to get the input field by id every time, it's already scoped as this
这也意味着您不需要每次都通过 id 获取输入字段,它已经被限定为这样
<input type="text" id="filterField" oninput="filter(this.value)"/>
回答by Tom Groves
The problem you have is that your text input and your function share a common name. Try renaming as follows
您遇到的问题是您的文本输入和您的函数共享一个通用名称。尝试重命名如下
<input type="text" id="filterText" oninput="filter(document.getElementById('filterText'))"/>
There are still some problems with your code, but I'll leave those for you to figure out, given this is a school assignment ;-)
您的代码仍然存在一些问题,但鉴于这是学校作业,我会将这些问题留给您解决 ;-)
回答by Miguel Angelo
See if this solves your problem
看看这是否能解决您的问题
<html>
<head>
<TITLE>Cancelled lessons</TITLE>
</head>
<body>
<script>
function fltr(text){
text = text.toString().toLowerCase();
for (i=0;i<lessonList.length;i++){
if(lessonList[i].innerHTML.toLowerCase().indexOf(text)==-1){
lessonList[i].style.display = "none";
}
else{
lessonList[i].style.display ="";
}
}
}
function gup( name )
{
name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
</script>
<form>
Filter: <input type="text" id="filter" oninput="fltr(document.getElementById('filter'))"/>
</form>
<div id="lessons">
<div class="entry"> MaA 11:00 C131 Ej NV3C</div>
</div>
<script>
var lessonList = document.getElementsByClassName("entry");
var filterField =document.getElementById("filter");
filterField.value = gup("class");
fltr(filterField.value);
</script>
</body>
</html>
Explanation:
解释:
You named a function with the same name of the input text-box id. When I changed the name of the function, it stopped the error.
您使用与输入文本框 ID 相同的名称命名了一个函数。当我更改函数名称时,它停止了错误。