jQuery自定义书面选择器 [英] Jquery custom written selector

查看:157
本文介绍了jQuery自定义书面选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我一直在尝试通过各种方式解决以下问题.我已经花了几个小时,似乎无法弄清楚该怎么做.

I've been trying to solve the following problem all sorts of ways today. I've spent hours on it and I can't seem to figure out how to do it.

基本问题是:我有一堆JavaScript逻辑来弄清楚以下变量是1还是0.

The basic problem is this: I have a bunch of JavaScript logic going on to figure out if the following variables should be a 1 or 0.

1)active
2)alternative
3)classic
4)vintage
etc.... 

,所有这些变量在我的设计中基本上都充当过滤器.我需要他们来确定是否应展示产品.因此,一旦确定了这些值,我便希望通过附加或删除CSS类来显示产品样式(或隐藏产品样式)(我们称其为.style).每个产品都有一个或多个连接的过滤器.因此,就像一双鞋子可能是经典的AND替代品一样,在这种情况下,经典OR替代变量中的1应该会导致鞋子以样式显示.

and all these variables act essentially as filters in my design. I need them to determine whether a product should be displayed or not. So once these values are determined, I want to display product styles accordingly (or hide product styles) by appending or removing a CSS class (let's call it .style) . Each product has 1 or multiple of the filters attached to it. So like a pair of shoes might be classic AND alternative, in which case a 1 in either classic OR alternative variables should cause the shoes to display with the style.

执行此显示方面的最佳方法是什么?我当时想写一个自定义选择器,但是我很难弄清楚如何编写它.

What's the best way to do this display aspect? I was thinking of writing a custom selector but I'm having a very hard time figuring out how to write it.

类似...

if($(".divProducts").hasClass(active) || $(".divProducts").hasClass(alternative) || etc..)
{
 ....?
}

但是我不会走这么远,因为首先,即使IF语句子句正确解析,我也不知道哪个产品实际上在替代或活动或任何其他过滤器中返回1.

but I don't get very far this way because first of all, even if the IF statement clause parses correctly, I won't know which product actually returned a 1 in alternative or active or any of the other filters.

这真的很难解释.

TLDR;基本上我需要这个:

如果某个div容器中的任何元素的任何过滤器的值为1,则对每个元素应用.style.

If ANY of the elements in some div container have ANY of the filters that are of value 1, apply .style to each of those elements.

我们将非常感谢您的帮助,因为这实际上是明天到期的项目的一部分...我没想到它会遇到这么麻烦:\

Help would be hugely appreciated as this is part of a project due tomorrow actually... I didn't expect to have such a hard time with it :\

顺便说一句,产品跟踪带有类的过滤器.所以举个例子 鞋类产品->

By the way, the products keep track of filters with classes. So for example shoes product->

<div class="blah blah vintageFilter blah blah">...</div>

我想将.style类应用于此div,如果vintage变量为1.有意义吗?

I want to apply .style class to this div IF vintage variable is 1. Make sense?

推荐答案

这应该有效:

// First clear the .style class from everything
$('.divProducts').removeClass('style');
// Then add it to the selected items
$('.divProducts.active, .divProducts.alternative').addClass('style');

这篇关于jQuery自定义书面选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆