简体   繁体   English

我如何轻松检查很多元素是否具有类(查询参数/字符串)?

[英]How can I check a lots of element has class easily (query parameter/ string)?

I hope to make a filtering function, for example, we have categories "a", "b", "c", "d", and "e". 我希望创建一个过滤功能,例如,我们有类别“ a”,“ b”,“ c”,“ d”和“ e”。

I allow user come to the page with query string. 我允许用户使用查询字符串来访问页面。 Such as http://example.com?cate=a , http://example.com?cate=b , etc. Categories sorting buttons are a group of radio button. http://example.com?cate=ahttp://example.com?cate=b等分类排序按钮是一组单选按钮。

Here are the code to active the categories sorting button: 这是激活类别排序按钮的代码:

function GetURLParameter(sParam) {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split("&");
        for (var i = 0; i < sURLVariables.length; i++) {
            var sParameterName = sURLVariables[i].split("=");
            if (sParameterName[0] == sParam) {
                return sParameterName[1];
            }
        }
    }
    var cate = GetURLParameter("cate");

    switch (cate) {
        case "a":
            $(window).load(function() {
                $("#aBtn").prop("checked", true);
            });
            break;
        case "b":
            $(window).load(function() {
                $("#bBtn").prop("checked", true);
            });
            break;
        case "c":
            $(window).load(function() {
                $("#cBtn").prop("checked", true);
            });
            break;
        case "d":
            $(window).load(function() {
                $("#dBtn").prop("checked", true);
            });
            break;
        case "e":
            $(window).load(function() {
                $("#eBtn").prop("checked", true);
            });
            break;
        default:
            break;
    }

Then, I hope to filter the objects that is or not match the cate "a", "b", "c", "d" and "e". 然后,我希望过滤出与类别“ a”,“ b”,“ c”,“ d”和“ e”不匹配的对象。 Some objects belong to more than one cates. 一些对象属于多个类别。 Objects have a common class, which is " .all ". 对象具有一个通用类,即“ .all ”。 If filtering active, the objects which is not belongs to the cate must add class " .none ". 如果激活过滤,则不属于该类别的对象必须添加类“ .none ”。

Here is the example: 这是示例:

<div class="all a e">I have a and e.</div><!-- if query string = a or e, it should show -->
<div class="all b e">I have b and e.</div><!-- if query string = b or e, it should show -->
<div class="all b c e">I have b, c and e.</div><!-- if query string = b, c or e, it should show -->
<div class="all c">I have c.</div><!-- if query string = e, it should show -->
<div class="all d">I have d.</div><!-- if query string = d, it should show -->
<div class="all d">I have d.</div><!-- if query string = d, it should show -->
<div class="all d e">I have d and e.</div><!-- if query string = d or e, it should show -->

Or should I add a unique id for each object? 还是应该为每个对象添加唯一的ID?

You could hide() them all and then show() the matching ones. 您可以全部hide()它们,然后show()匹配的对象。

$(".all").hide();
$("."+cate).show();

If you really wish to add the class none : 如果你真的想添加类none

$(".all").addClass("none");
$("."+cate).removeClass("none");

I made a little demo on CodePen where I "simulate" the cate value via a text input. 我在CodePen上做了一个小样,通过文本输入“模拟”了cate值。
;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM