[英]jQuery event not defined on mouseover/mouseout
嗨,我有一個使用jQuery 1.3.2可以正常工作的函數,但是現在我正在嘗試以1.4.2運行它,並且出現以下錯誤“未定義事件”。 這是簡化的代碼:
<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可見,並在其周圍放置一行,顯示正在滾動的縣名。
firebug中的錯誤中斷位於定義mytext變量的行上。 但我懷疑問題在於以下語法問題:if(event.type ==“ mouseover”)
謝謝,麗茲
您需要命名事件參數,如下所示:
$('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');
});
});
它並不完全相同 ,但通常要好一些,它使用mouseenter
和mouseleave
而不是mouseover
和mouseout
(因此在進入/離開孩子時不會觸發)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.