2010-10-04 237 views
0

您好我有一個使用jQuery 1.3.2工作正常的功能,但現在我試圖運行它與1.4.2,我得到以下錯誤「事件未定義」。 這裏是簡化了代碼:沒有在鼠標懸停/鼠標懸停定義jQuery事件

<div id="mapcontainer><img href="" usemap="#rage_image_map"> 
    <div id="mytext"></div> 
</div> 
<map name="rage_image_map"> 
    <area shape="poly" href="asthma-data/county-asthma-profiles/del-norte-county-asthma-profile" coords="12,7,10,12" alt="Del Norte County"> 
    <area shape="poly" href="asthma-data/county-asthma-profiles/siskyou-county-asthma-profile" coords="42,3,42,6,40,8,36,11" alt="Siskyou County"> 
</map> 

這裏是功能:

$(function() { 
    $('area').bind('mouseover mouseleave', function() { 
     var mytext = $('#mytext'); 
     if (event.type == "mouseover") { 
      var countyname = $(this).attr("alt"); 
      mytext.html(countyname); 
      mytext.addClass('textcontainer'); 
     } else { 
      mytext.text(''); 
      mytext.removeClass('textcontainer'); 
     } 
    }) 
}); 

類使得DIV可見mytext的內容,並把線周圍,顯示縣名滑過。

螢火蟲中的錯誤斷點在定義mytext變量的行上。但我懷疑問題是低於語法問題:如果(event.type ==「鼠標懸停」)

感謝,利茲

回答

5

您需要命名事件參數,就像這樣:

$('area').bind('mouseover mouseleave', function(event){ 
               ^add this 

這是一個有點更容易使用.hover()不過,像這樣:

$(function() { 
    $('area').hover(function(){ 
    var countyname = $(this).attr("alt"); 
    $('#mytext').html(countyname).addClass('textcontainer'); 
    }, function() { 
    $('#mytext').text('').removeClass('textcontainer'); 
    }); 
}); 

這不正是相同,但好一點通常情況下,它使用mouseentermouseleave而不是mouseovermouseout(所以它不會在進入/離開兒童時觸發)。

+0

謝謝,我的印象是綁定更好。這清除了差異。在這種情況下,有和將沒有孩子...但這是一個獨特的情況...通常他們做.. – liz 2010-10-04 17:34:16

+0

@liz - 這種方法適用於/不帶孩子,我希望你會同意更容易在眼睛:)我傾向於堅持下去,但每個自己的,無論工程:) – 2010-10-04 17:37:06

+0

絕對同意。 – liz 2010-10-05 15:50:33