2014-10-11 51 views
1

我在網站上創建了一個快速選項卡部分。點擊時,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"); 
})` 

回答

1

你可以使用add()方法來實現這一目標:

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

Example Here


你試圖:

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

這基本上等同於使用:

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

這就是爲什麼它不工作。

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

+1

這工作!謝謝!我發誓我嘗試過。 – user2504370 2014-10-11 19:09:22

0

我認爲:

$(this, target) 

將試圖找到節點元素(這個參考)目標上下文中。但它不會找到它,因爲目標不包含該元素