TypeError:object不是函数 [英] TypeError: object is not a function

查看:73
本文介绍了TypeError:object不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的学校开发这个webapp。页面应该通过url参数class过滤条目。这可以正常工作,但是当我尝试更改过滤器时,它会给出TypeError:object is not a function。我究竟做错了什么?

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?

<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>


推荐答案

看起来oninput处理程序调用过滤器函数来自表单的范围(document.forms [0])而不是全局。如果检查document.forms [0] .filter的值,它将返回输入标记。您只需要确保函数名称与输入名称/ id不同。

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.

这也意味着您不需要每个按id获取输入字段时间,它已经确定为这个

This also means you don't need to get the input field by id every time, it's already scoped as this

<input type="text" id="filterField" oninput="filter(this.value)"/>

这篇关于TypeError:object不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆