2012-08-03 64 views
0

我有這樣的代碼:jQuery的隱藏DIV功能工作不正確

jQuery('.hoverbgpfthnailiface').click(function(e){ 
    var target = e.target; 

    while (target.nodeType != 1) target = target.parentNode; 
    if(target.tagName != 'TEXTAREA'){ 
     jQuery('.header-search-form').css({ 'display' : 'none' }); 
    } 
}); 

奇怪的是,如果我把它放在Chrome的控制檯它的工作,但是當我將其添加在我的文件不工作,不在控制檯中顯示任何錯誤。 任何想法爲什麼會發生這種情況?

回答

1

你有沒有嘗試把代碼放入就緒函數?

jQuery(document).ready(function() { 

    jQuery('.hoverbgpfthnailiface').click(function(e){ 
     var target = e.target; 

     while (target.nodeType != 1) target = target.parentNode; 
     if(target.tagName != 'TEXTAREA'){ 
       jQuery('.header-search-form').css({ 'display' : 'none' }); 
     } 
    }); 

}); 

告訴我,如果爲你工作:)

編輯 只有一件事,如果你有興趣,在當綁定的函數被調用一個jQuery事件,this是一樣的e.target

EDIT2 你與類的hoverbgpfthnailiface存在的元素之前執行你的代碼。 您正在第2716行創建它們,並且因爲$(document).ready低於您在稍後執行的問題的代碼中使用的那個。

所以我建議你嘗試從線103代碼移動到2717

+0

不是真的,因爲我已經準備好了這個功能,我發佈的只是代碼中的一部分 – agis 2012-08-03 15:05:42

+0

好的,我需要更多信息。你有沒有嘗試過使用'console.log'來查看變量的值?現成的建議是因爲它可能發生,當您將腳本添加到文件時,元素不在頁面中,但是它們在後面。 – NicoSantangelo 2012-08-03 15:10:40

+0

我不知道如何使用console.log這個特定的問題,如果你想看看我的網站是[here](http://ibizapartypeople.com)和包含此代碼是在行71如果你查看源 – agis 2012-08-03 15:29:47

0

你不一定必須使用jQuery的功能,你可以使用基本的JS:

嘗試:

document.getElementById('your element name').style.display = 'none'; 

讓我知道這是否有幫助。