2012-06-21 55 views
0

我有2 jquery的瓦爾:如何將多個變量結合成一個選擇

var contVar = $('#container'); 
var linkNameVar = $(this).attr('id'); //for example <div id="test"> 

我需要的是把它們結合起來才能訪問#container .test,即它看起來像:

$(contVar .linkNameVar) 

如何我:

  1. linkName前面加一個.把它變成一個類。

  2. 然後將2個變量組合到1個選擇器中。

我看着.add()文檔,但它似乎並不是我所需要的。

回答

0

試試這個,你可以使用第二個參數作爲選擇器的上下文。

$('.'+linkNameVar, contVar) 
0

這應該工作:

contvar.find('.' + linkNameVar) 
+0

謝謝你,這個工作也很好,我選擇了其他答案,因爲他們先回答了我。 – muudless

0
var yourNode = $('.' + linkNameVar, contVar) 
0

如果<div id-"test"><div id="container">一個孩子,那麼你要做的: -

var contVar = $('#container'); 
var linkNameVar = $(this).attr('id'); //for example <div id="test"> 
var linkNameVar1 = $(this).attr('class'); // for example <div class="test"> 

contVar.children('#'+linkNameVar); // if test is an ID then like this 
contVar.children('.'+linkNameVar1); // if test is a class then like this 

讓我知道如果有任何混亂或者你有其他的要求。

相關問題