【问题标题】:how to perform jQuery live filter on divs and not list items?如何在 div 上执行 jQuery 实时过滤器而不是列出项目?
【发布时间】:2012-12-07 22:51:35
【问题描述】:

我认为我正在寻找的功能称为“实时过滤器”,我正在尝试过滤输出的数据库条目,其中每个条目具有以下结构:

<div class="class1 class2 class3" data-name="name" data-date="MM/DD/YY">
    <img src="path/to/image.jpg">
    <div class="class4">
        <h4>some text here</h4> <!-- this is what i want to search in -->
        <div class="class5">
            <span id="date">date: MM/DD/YY</span>
            <span id="id_one">text</span>
        </div>
        <div class="class6">
            <span id="id_two">text</span>
            <span id="id_three">text</span>
        </div>
        <span id="id_four">text</span>
        <span id="id_five"><a class= class7 href="path/to/link.html">link</a></span>
    </div>
</div>

我想在 .class4 h4 中搜索,然后淡出所有不包含搜索文本的 .class1 div。

这是我遇到的解决方案的默认行为(主要是here),但它们似乎只适用于列表项而不适用于 div。例如,我尝试了here的代码并将其修改为:

<script>
    $(document).ready(function() {
        $("#filter").keyup(function() {
            // Retrieve the input field text and reset the count to zero
            var filter = $(this).val(), count = 0;

            // Loop through the comment list
            $(".class4 h4").each(function() {
                // If the list item does not contain the text phrase fade it out
                if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                    $(".class1").fadeOut();
                    // Show the list item if the phrase matches and increase the count by 1
                } 
                else {
                    $(this).show();
                    count++;
                }
            });

            // Update the count
            var numberItems = count;
            $("#filter-count").text("# of matches = "+count);
        });
    });
</script>

它搜索正确,但它用.class1 隐藏了所有的div。

有谁知道如何使用实时过滤器在.class4 h4 中搜索特定文本,然后用.class1 淡出不包含搜索文本的div?

谢谢。

【问题讨论】:

    标签: jquery html filter


    【解决方案1】:

    使用onkeypress

    例如:

     <input type="text" onkeypress=" return callfunction(event); "
    

    然后你可以在调用函数中实现淡出逻辑。

    调用函数应该是这样的:

    function callfunction(event){   
    // Loop through the comment list
                $(".class4 h4").each(function() {
                    // If the list item does not contain the text phrase fade it out
                    if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                        $(".class1").fadeOut();
                        // Show the list item if the phrase matches and increase the count by 1
                    } 
                    else {
                        $(this).show();
                        count++;
                    }
                });
    
                // Update the count
                var numberItems = count;
                $("#filter-count").text("# of matches = "+count);}          
    }                        
    

    并且该功能不需要在文档就绪方法中

    【讨论】:

    • 感谢您的回复,我将上面的代码添加到我的输入行,所需的功能不起作用。谢谢。
    • 它在每次调用时都达到功能没有?在新版本的 callfunction 中放置一个警报,并在每次键入时检查它是否输入。如果它每隔一段时间就会发出警报,那么没关系,但你的逻辑不好。
    • 你好,我按照上面的代码试过了,但是没有用,谢谢。
    • 你好,我会在 4-5 小时内给你做一个现场演示,如果我忘记了请提醒我,因为我很忙。我向你保证它会起作用:)
    【解决方案2】:

    正如我告诉你的,我带着现场演示回来了。代码如下:

    ***只要确保包含 jquery 脚本即可。

    <label>Type here</label>
    
    <input type="text" id="filter" onkeyup=" return callfunction(); " />
    
    <div>
        <div class="class1 class2 class3" data-name="name" data-date="MM/DD/YY">
            <img  src="@Url.Content("~/StaticContent/Images/cycling_logo.gif")" width="90" height="90">
            <div class="class4">
                <h4>abcdef</h4> <!-- this is what i want to search in -->
                <div class="class5">
                    <span id="date">date: MM/DD/YY</span>
                    <span id="id_one">text</span>
                </div>
                <div class="class6">
                    <span id="id_two">text</span>
                    <span id="id_three">text</span>
                </div>
                <span id="id_four">text</span>
                <span id="id_five"><a class= class7 href="path/to/link.html">link</a></span>
            </div>
        </div>
    
        <div class="class1 class2 class3" data-name="name" data-date="MM/DD/YY">
            <img  src="@Url.Content("~/StaticContent/Images/cycling_logo.gif")" width="90" height="90">
            <div class="class4">
                <h4>some</h4> <!-- this is what i want to search in -->
                <div class="class5">
                    <span id="date">date: MM/DD/YY</span>
                    <span id="id_one">text</span>
                </div>
                <div class="class6">
                    <span id="id_two">text</span>
                    <span id="id_three">text</span>
                </div>
                <span id="id_four">text</span>
                <span id="id_five"><a class= class7 href="path/to/link.html">link</a></span>
            </div>
        </div>
    
        <div class="class1 class2 class3" data-name="name" data-date="MM/DD/YY">
            <img  src="@Url.Content("~/StaticContent/Images/cycling_logo.gif")" width="90" height="90">
            <div class="class4">
                <h4>dcdef</h4> <!-- this is what i want to search in -->
                <div class="class5">
                    <span id="date">date: MM/DD/YY</span>
                    <span id="id_one">text</span>
                </div>
                <div class="class6">
                    <span id="id_two">text</span>
                    <span id="id_three">text</span>
                </div>
                <span id="id_four">text</span>
                <span id="id_five"><a class= class7 href="path/to/link.html">link</a></span>
            </div>
        </div>
    
        <div class="class1 class2 class3" data-name="name" data-date="MM/DD/YY">
            <img  src="@Url.Content("~/StaticContent/Images/cycling_logo.gif")" width="90" height="90">
            <div class="class4">
                <h4>dcfffff</h4> <!-- this is what i want to search in -->
                <div class="class5">
                    <span id="date">date: MM/DD/YY</span>
                    <span id="id_one">text</span>
                </div>
                <div class="class6">
                    <span id="id_two">text</span>
                    <span id="id_three">text</span>
                </div>
                <span id="id_four">text</span>
                <span id="id_five"><a class= class7 href="path/to/link.html">link</a></span>
            </div>
        </div>
    
        <div class="class1 class2 class3" data-name="name" data-date="MM/DD/YY">
            <img  src="@Url.Content("~/StaticContent/Images/cycling_logo.gif")" width="90" height="90">
            <div class="class4">
                <h4>dssfafafsa</h4> <!-- this is what i want to search in -->
                <div class="class5">
                    <span id="date">date: MM/DD/YY</span>
                    <span id="id_one">text</span>
                </div>
                <div class="class6">
                    <span id="id_two">text</span>
                    <span id="id_three">text</span>
                </div>
                <span id="id_four">text</span>
                <span id="id_five"><a class= class7 href="path/to/link.html">link</a></span>
            </div>
        </div>
    
    </div>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/latest/jquery.js"></script>
    <script type="text/javascript">
        function callfunction() {
            $(".class4 h4").each(function() {
                if ($(this).text().indexOf($("#filter").val()) != 0) {
                    $(this).parent().parent().fadeOut(500);
                } else {
                    $(this).parent().parent().fadeIn(500);
                }
            });
        }
    </script>
    

    【讨论】:

    • 非常感谢,我将在大约 8 小时内对此进行测试并会回复,谢谢您的宝贵时间。
    • 你好,我已经根据上述创建了 jsfiddle,但它似乎没有运行。 jsfiddle.net/rwone/SuEQC。谢谢。
    • jsfiddle 有问题:jsfiddle.net/slown1/SuEQC/4 已解决 :) 将第一行替换为:" window.callfunction = function() {//函数中的代码
    • 谢谢。在我的实时测试站点上,它逐渐消失的程度超过了所需的 div,无论如何,是否只针对具有 .class1 的 div?只是为了清楚起见,因为我今天要签收,如操作中的“我想在 .class4 h4 内搜索,然后淡出所有不包含搜索文本的 .class1 div”。谢谢。
    • ps,下面会导致所有class1的div不管是否包含搜索到的文本都会淡出:`function callfunction() { $(".class4 h4").each(function () { if ($(this).text().indexOf($("#filter").val()) != 0) { $(".class1").fadeOut(500); } else { $ (".class1").fadeIn(500); } }); }`
    【解决方案3】:

    这个问题有你想要的答案:jQuery - Trying to filter divs on keyup

    刚刚在我自己的网站上使用过,效果很好。

    【讨论】:

    • 感谢您的这个想法。它似乎实现了针对 div 的父方法。在我的例子中,它似乎不能正常工作,因为我正在使用的 div 容器有很多事情要做。如果它们不包含搜索到的文本,是否有专门针对我想要淡出的div?谢谢你。 ps 表示我理解您的建议和概念,这是该方法的一个 jsfiddle jsfiddle.net/rwone/Tsvfc 谢谢。
    • 我正在尝试这种变化,但还没有工作 - jsfiddle.net/rwone/Tsvfc/1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2016-05-26
    • 2014-04-30
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多