gpt4 book ai didi

javascript - 使用 this 和变量的多个选择器

转载 作者:行者123 更新时间:2023-11-28 00:55:11 25 4
gpt4 key购买 nike

我在网站上创建了一个快速选项卡部分。单击时,jQuery 会抓取数据幻灯片并使用该信息来查找匹配的 ID 并应用一个类。

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

当我在同一行上使用“this”和目标时,“目标”没有应用到它的事件类。

$(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");

Example Here

<小时/>

您正在尝试:

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

这基本上相当于使用:

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

这就是它不起作用的原因。

您可以看到一个演示此的示例 here .

关于javascript - 使用 this 和变量的多个选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26318175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com