2013-06-11 86 views
0

我想爲每個類寫東西選擇這個標籤並且不顯示它。

$('.no_show_on_form').each(function() 
    { 
     $(this label).css('display','none'); 
    }); 

它實際上返回false。

我該如何正確編寫我的語法?

-

編輯#1:我曾嘗試用最接近()函數這個其他方式,但它似乎並不返回錯誤或任何作品。

$('.no_show_on_form').each(function() 
{ 
    $(this).closest('label').css('display','none'); 
}); 
+0

添加代碼來理解這個問題 – Amit

回答

4

好像你正在尋找這一點 -

$('label',this).css('display','none'); 
+0

嗨@pXL,我使用了上面的解決方案,因爲我更熟悉這個解決方案而不是你的。但是,我相信這有助於別人。 –

1

選擇器是字符串。

你需要調用jQuery的遍歷方法,探索元素:

$(this).find('label') 
+0

嗨@Slaks,您的解決方案實際工作得很好。 Thx –

1

如果.no_show_on_form是某種容器中,然後使用情境:

$('label', this).hide(); 

如果.no_show_on_form是表單元素,你需要更有創意一點:

$('.no_show_on_form').each(function(i,el) { 
    if (el.id) { 
     $('label[for="' + el.id + '"]').hide(); 
    } 
}); 
+0

這是一種容器。這兩種方法都工作得很好。 –

0
$('.no_show_on_form').each(function() 
{ 
    $(this).find('label').css('display','none'); 
}); 

正確的語法是使用功能查找()。

描述 獲取當前匹配元素集中每個元素的後代,由選擇器,jQuery對象或元素過濾。

http://api.jquery.com/find/