簡體   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