使用解析器进行Javascript搜索

使用解析器进行Javascript搜索,javascript,html,Javascript,Html,我想让这段代码与我的解析器一起工作 function search() { var input, filter, table, tr, td, i; input = document.getElementById("input"); filter = input.value.toUpperCase(); table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); for (i = 0; i

我想让这段代码与我的解析器一起工作

function search() {
var input, filter, table, tr, td, i;
input = document.getElementById("input");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
        if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
        } else {
            tr[i].style.display = "none";
        }
    }
}
有什么想法吗

<table id='mytable'>
当您为评估
时,使用
tr
依此类推。(k)

for(i=0,k=tr?tr.length:0;i
您的解析器脚本是什么?您有id为myTable的表吗?如果您没有显示代码的相关部分,我们应该如何帮助您?sry我认为这与此无关。我会把我的信寄出去,只是因为你提到了。我也不确定这是否相关。如果没有,你为什么提到它,为什么它在问题的上下文中很重要?事实上,我认为表更重要,因为问题似乎是
document.getElementById(“myTable”)
返回
null
。但是如果在html中构建相同的表,它就可以工作。解析器现在在帖子中。看到您的代码,您正在给出表和
mytable
id
,而脚本正在尝试获取
mytable
id
s是区分大小写的。或者,你知道,只需修复键入错误。这不是更健壮,当你试图获取
tr.length
时,这将进一步崩溃,通常只是停止工作。使用太多的回退是不好的,因为它会使代码难以调试。让它抛出该错误,以便您第一眼就知道名称不正确。(这就是为什么您需要首先测试自己的代码)。在使用动态名称时,这更合适。我已经提到了,但只是根据建议对其进行了编辑。
<input class="pl-search" type="text" placeholder="Suche" id="input" onkeyup="search()">
Uncaught TypeError: Cannot read property 'getElementsByTagName' of null
<table id='mytable'>
<table id='myTable'>
tr = table ? table.getElementsByTagName("tr") : null;
for (i = 0, k= tr ? tr.length : 0; i < k; i++)