jQuery/JavaScript:在jQuery中使用JavaScript数组

jQuery/JavaScript:在jQuery中使用JavaScript数组,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我对jQuery非常陌生,我有以下问题: 我制作了一个JS脚本,它将用户输入与表示电影标题的一组元素进行比较 userinput = userinput.toLowerCase(); var list = document.getElementsByTagName("h3"); //dit wordt de lijst met titels var i = list.length, html, flag = false, matches = []; while (i--)/

我对jQuery非常陌生,我有以下问题:

我制作了一个JS脚本,它将用户输入与表示电影标题的一组
元素进行比较

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();

  }
}
但是我真的不知道如何在jQuery中引用
匹配[I]
数组!我尝试了
#匹配[I]
之类的东西,但似乎不起作用。你们谁能帮助我

谢谢

您需要

$(“#overzicht_列表”).children().filter(函数(){
返回$(this).text().indexOf(匹配[i])<-1;
}).remove();
您需要

$(“#overzicht_列表”).children().filter(函数(){
返回$(this).text().indexOf(匹配[i])<-1;
}).remove();

我尝试用jQuery编写脚本,并尝试删除无用的循环。 我希望我的脚本不会改变您脚本的行为:

在文本中查找带有
userinout
var的
h3
元素,并将其从DOM中删除(在循环期间或之后),如果没有匹配的脚本,则发出警报

第1版

或者,下一个脚本相同,但直接删除第一个循环中匹配的元素:

第2版


。。。现在您没有
matches
变量,因为它是无用的,当您从DOM中删除元素时,您将删除
matches

中的引用。我尝试在jQuery中编写脚本,并尝试删除无用的循环。 我希望我的脚本不会改变您脚本的行为:

在文本中查找带有
userinout
var的
h3
元素,并将其从DOM中删除(在循环期间或之后),如果没有匹配的脚本,则发出警报

第1版

或者,下一个脚本相同,但直接删除第一个循环中匹配的元素:

第2版


。。。现在您没有
匹配项
变量,因为它是无用的,当您从DOM中删除元素时,您会删除
匹配项中的引用

我想您还没有完全理解我的问题;)我要做的是检查所有元素的用户输入,如果存在匹配项,则删除容器中除匹配项之外的所有内容。您的脚本的作用正好相反,即只删除匹配项。我认为您没有完全理解我的问题;)我要做的是检查所有元素的用户输入,如果存在匹配项,则删除容器中除匹配项之外的所有内容。您的脚本执行的操作正好相反,即只删除匹配项。此代码似乎尚未运行。。。我可以添加这个而不是我的$(“#overzicht_list”).children(“:not(“”)”).remove();?另外,您是否可以详细说明您的.filter(函数)正在检查什么?此代码似乎还没有工作。。。我可以添加这个而不是我的$(“#overzicht_list”).children(“:not(“”)”).remove();?另外,您是否可以详细说明您的.filter(函数)正在检查什么?
$("#overzicht_list").children(":not('')").remove();
$("#overzicht_list").children().filter(function(){
   return $(this).text().indexOf(matches[i]) < -1;
}).remove();
var userinput = userinput.toLowerCase(), 
    flag = false;

//select and loop all H3 (with jQuery)
$("h3").each(function(){

    //if text inside H3 match with userinput
    if($(this).text().indexOf(userinput)  >= 0){
        //add class matched to the matched element
        $(this).addClass("matched");

        //flat to true
        flag = true;
    }
});

//check flag (if true remove al matched element)
if(flag)$("#overzicht_list .matched").remove();
else alert("No match found!");
var userinput = userinput.toLowerCase(),
    flag = false;
$("h3").each(function(){
    if($(this).text().indexOf(userinput) >= 0){

        //directly remove matched element
        $(this).remove();
        flag = true;
    }
});

//if no one matched alert
if(!flag) alert("No match found!");