jQuery:类和 .each()

发布于 2025-01-04 08:44:55 字数 900 浏览 0 评论 0原文

我正在尝试将类添加到 div (.views-row) 内的元素。我遇到的问题是 .views-row 在页面上重复,这是不可避免的,并且我尝试设置样式的元素最终都具有相同的类,无论它们是否应该。代码可能更好地解释了它:

$(".views-row").each(function(){
        if ($((this)" .field-name-field-warning:contains('High')").length) {
            $((this)" .field-name-field-warning").addClass("fault-high");
            $((this)" .field-name-field-warning .field-label").addClass("disappear");
            $((this)" .field-name-field-warning .field-items").addClass("disappear");
        }
        if ($((this) ".field-name-field-warning:contains('Moderate')").length) {
            $((this) ".field-name-field-warning").addClass("fault-moderate");
            $((this) ".field-name-field-warning .field-label").addClass("disappear");
            $((this) ".field-name-field-warning .field-items").addClass("disappear");
        }
});

我想我在(​​这个)部分遇到了麻烦。有什么想法吗?

谢谢。

I'm trying to add classes to elements within a div (.views-row). The problem I'm having is that the .views-row repeats on the page, which is unavoidable, and the elements I'm attempting to style all end up the same classes regardless of whether they should or not. The code probably explains it better:

$(".views-row").each(function(){
        if ($((this)" .field-name-field-warning:contains('High')").length) {
            $((this)" .field-name-field-warning").addClass("fault-high");
            $((this)" .field-name-field-warning .field-label").addClass("disappear");
            $((this)" .field-name-field-warning .field-items").addClass("disappear");
        }
        if ($((this) ".field-name-field-warning:contains('Moderate')").length) {
            $((this) ".field-name-field-warning").addClass("fault-moderate");
            $((this) ".field-name-field-warning .field-label").addClass("disappear");
            $((this) ".field-name-field-warning .field-items").addClass("disappear");
        }
});

I guess I'm having trouble with the (this) part. Any thoughts?

Thank-you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谈下烟灰 2025-01-11 08:44:56

如果您想选择当前元素中的任何元素,有两种方法。

使用 $().find()

$(this).find(".field-name-field-warning")

通过提供上下文(实际上与 find 相同) :

$(".field-name-field-warning", $(this))

If you want to select any element within your current one there are two ways.

Using $().find():

$(this).find(".field-name-field-warning")

By providing a context (which does practically the same as find):

$(".field-name-field-warning", $(this))
本王不退位尔等都是臣 2025-01-11 08:44:56

使用 .find()

$(this).find("selector");

use .find() on the this :

$(this).find("selector");
梦在夏天 2025-01-11 08:44:56

当 .addClass() 将为您迭代所有元素时,无需在此处使用 .each() 。

您可以从像这样的方法开始:

var containsHigh = $(".views-row .field-name-field-warning:contains('High')")
containsHigh.addClass('fault-high');
containsHigh.find('.field-label, .field-items').addClass('disappear');

var containsModerate = $(".views-row .field-name-field-warning:contains('Moderate')")
containsModerate.addClass('fault-moderate');
containsModerate.find('.field-label, .field-items').addClass('disappear');

然后通过像这样的链接方法变得更奇特:

var warnings = $(".views-row .field-name-field-warning");
warnings.filter(":contains('High')")
    .addClass('fault-high')
    .find('.field-label, .field-items').addClass('disappear');

warnings.filter(":contains('Moderate')")
    .addClass('fault-moderate')
    .find('.field-label, .field-items').addClass('disappear');

There's no need to use .each() here when .addClass() will iterate over all elements for you anyway.

You can start by moving to something like this:

var containsHigh = $(".views-row .field-name-field-warning:contains('High')")
containsHigh.addClass('fault-high');
containsHigh.find('.field-label, .field-items').addClass('disappear');

var containsModerate = $(".views-row .field-name-field-warning:contains('Moderate')")
containsModerate.addClass('fault-moderate');
containsModerate.find('.field-label, .field-items').addClass('disappear');

Then get even fancier by chaining methods like this:

var warnings = $(".views-row .field-name-field-warning");
warnings.filter(":contains('High')")
    .addClass('fault-high')
    .find('.field-label, .field-items').addClass('disappear');

warnings.filter(":contains('Moderate')")
    .addClass('fault-moderate')
    .find('.field-label, .field-items').addClass('disappear');
秋凉 2025-01-11 08:44:56

看起来您正在尝试使用 this 在元素中查找元素。您应该find 方法和filter 来过滤匹配元素集中的元素。

这是完整的简化代码。

$(".views-row").each(function(){
     var $fieldWarning = $(this).find(".field-name-field-warning");
     if($fieldWarning.length){
        if ($fieldWarning.filter(":contains('High')").length) {
            $fieldWarning.addClass("fault-high");
            $fieldWarning.find(".field-label").addClass("disappear");
            $fieldWarning.find(".field-items").addClass("disappear");
        }
        if ($fieldWarning.filter(":contains('Moderate')").length) {
            $fieldWarning.addClass("fault-moderate");
            $fieldWarning.find(".field-label").addClass("disappear");
            $fieldWarning.find(".field-items").addClass("disappear");
        }
     }
});

参考:.find().filter()

Looks like you are trying to find element within an element using this. You should find method for that and filter to filter the element in the matched set of elements.

Here is the complete simplified code.

$(".views-row").each(function(){
     var $fieldWarning = $(this).find(".field-name-field-warning");
     if($fieldWarning.length){
        if ($fieldWarning.filter(":contains('High')").length) {
            $fieldWarning.addClass("fault-high");
            $fieldWarning.find(".field-label").addClass("disappear");
            $fieldWarning.find(".field-items").addClass("disappear");
        }
        if ($fieldWarning.filter(":contains('Moderate')").length) {
            $fieldWarning.addClass("fault-moderate");
            $fieldWarning.find(".field-label").addClass("disappear");
            $fieldWarning.find(".field-items").addClass("disappear");
        }
     }
});

Reference: .find(), .filter()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文