繁体   English   中英

使用此和变量的多个选择器

[英]Multiple selectors using this and variable

我在网站上创建了一个快速标签栏。 单击时,jQuery抓取数据幻灯片并使用该信息查找匹配的ID并应用一个类。

<div class="service-link" data-slide="#post-123">Click Me</div>

当我在同一行上使用“ this”和target时,“ target”没有将活动类应用于它。

$(this, target).addClass("active");

如果我使用两行,它将起作用。 有人知道我为什么不能使用一行吗?

$(this).addClass("active");
$(target).addClass("active");

完整的工作脚本-

$(".service-link").click(function(){
    var target = $(this).data("slide");
    $(".service-type-slide, .service-link").removeClass("active");
    $(target).addClass("active");
    $(this).addClass("active");
})`

您可以使用add()方法实现这一点:

$(this).add(target).addClass("active");

这里的例子


您正在尝试:

$(this, target).addClass("active");

这基本上等同于使用:

$(target).find(this).addClass("active");

这就是为什么它不起作用。

您可以在此处看到一个演示此示例的示例。

我觉得:

   $(this, target)

将尝试在目标上下文中找到节点元素( 节点指代)。 但它不会找到它,因为目标不包含该元素

暂无
暂无

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

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