Rob*_*eld 1 html javascript arrays jquery
我是jQuery的新手,我有以下问题:
我制作了一个JS脚本,将用户输入与一组<h3>代表电影片头的元素进行比较.
userinput = userinput.toLowerCase();
var list = document.getElementsByTagName("h3"); //dit wordt de lijst met titels
var i = list.length,
html, flag = false,
matches = [];
while (i--)//here, we loop through the list of <h3>'s
{
var html = list[i].innerHTML.toLowerCase();
if (-1 < html.indexOf(userinput))
{
flag = true;
matches.push(list[i]);
list[i].className = "matched";
}
}
if (!flag)//no match
{if (-1 !== html.indexOf(userinput))
alert('No match found !');
}
else//match!
{
for (var i = 0; i < matches.length; i++)
{
$("#overzicht_list").children(":not('')").remove();
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我首先声明了一些我将要使用的变量.然后,我循环遍历<h3>元素列表,如果其中一个元素与用户输入匹配,我想执行以下操作.
我的容器中的所有元素overzicht_list但是<h3>应该删除匹配的元素.我尝试使用以下jQuery:
$("#overzicht_list").children(":not('')").remove();
Run Code Online (Sandbox Code Playgroud)
但我真的无法弄清楚如何matches[i]在jQuery中引用数组!我试过#matches[i]这样的东西,但这似乎不起作用.你们有人可以帮助我吗?
谢谢!
你需要过滤
$("#overzicht_list").children().filter(function(){
return $(this).text().indexOf(matches[i]) < -1;
}).remove();
Run Code Online (Sandbox Code Playgroud)