2012-07-02 76 views
0

我不知道該如何定位我想在這種情況下,元素:靶向組分

var $div = $(".row-container").filter(function() { 
     return $(this).data("i") == product_id; 
     // where value == product id to find 
     }); 

現在,內$div是具有類'price-row'這就是一個元素我想要。 $div.hasClass('price-row')只返回true或false值。

+0

請你更新你的問題來顯示一些例子的HTML?我一個人不太明白你想要做什麼。你的最後一句話聽起來像'$ div'通過現有的'.filter()'方法正確設置,但現在你想要找到一個子元素?或者是如何讓'.filter()'做你需要的? 「碎片」概念到哪裏去了? – nnnnnn

回答

3

使用.find如果它是一個子元素:

var $div = $(".row-container").filter(function() { 
    return $(this).data("i") == product_id; // where value == product id to find 
}).find('.price-row');

如果你的意思是,匹配的元素將是集中的,它包含在標準開始:

var $div = $(".row-container.price-row").filter(function() { 
    return $(this).data("i") == product_id; // where value == product id to find 
});
+0

不應該是:$(「。row-container .price-row」)?在沒有聯繫的情況下,因爲它是一個孩子? – Joey

+2

@Joey:不。如果它是一個子元素,它應該是'find'。或者,至少,這就是我所說的問題的意思,特別是:*「現在,在$ div內是一個具有」price-row「類的元素,這正是我想要的。」*因此,無論是具有'price-row'類(例子2)的'$ div'集合或具有'price-row'類(例子1)的'$ div'集合中的任何元素的子元素。 – Ryan