2010-08-02 66 views
2

我有頁a產品和它的圖像加載在一個表中。圖像最初是隱藏的,onmouseover我想顯示圖像。圖像是在div標籤內,產品名稱顯示爲link.In的onready功能,我的事件處理程序附加到鏈接,jquery - 訪問一個元素是給零null

$('a').each(function() 
    { 
     if($(this).attr('id').match(/prod/)) 
     { 
      $(this).mouseover(display()); 
     } 
    }); 

,並在事件處理程序(一個函數調用顯示)我打電話

function display() 
{ 
    $('div').each(function() 
    { 
     if($(this).attr('id').match(/sx/)) 
     { 
      alert("hi") 
     } 
    }); 
} 

但是,我出現錯誤$(「div」)爲空

HTML是:

<table> 
<tr><td><a href="link">product name</a></td> 
    <td><div class='.hidden'><table><tr><td><img src=""></img></td></tr></table></div></td></table> 
+0

什麼是你的HTML? – 2010-08-02 07:14:12

+0

更新我的帖子進一步 – 2010-08-03 08:49:07

回答

2

更換

$(this).mouseover(display()); 

$(this).mouseover(display); 

執行代碼的功能display()並傳遞它的返回值mouseover。但是你需要傳遞一個函數引用。

0

使顯示器功能匿名的,就像你在夜代碼的其餘部分HVE完成:

$('a').each(function() 
    { 
     if($(this).attr('id').match(/prod/)) 
     { 
      $(this).mouseover(function() { 
       $('div').each(function() 
       { 
        if($(this).attr('id').match(/sx/)) 
        { 
         alert("hi") 
        } 
       }); 
      }); 
     } 
    }); 

編輯:

關於$("div")爲空,你可以進一步澄清。你能告訴我下面的一些東西:

var obj = $("div");  //is obj null? 
alert($("div").length); //do you get a number? If so, what is it? 
+0

我試過這個,但說$(「div」)爲空 – jess 2010-08-02 09:26:30