由jquery设置的mouseover不在ie7中工作

时间:2014-02-20 09:14:00

标签: javascript jquery

我需要在图像上的onmouseover区域设置调用函数active(),我尝试使用jquery设置onmouseover,这适用于所有浏览器,但不适用于 IE7 所以请任何人建议我提示在 IE7

上使用此代码
$(document).ready(function(){

         var i = 1; 
        $("#map area").each(function(){
            var Id = $(this).attr('id');
            $(this).attr('onmouseover', "active('area"+i+"','"+Id+"',"+i+")");
            i++
        });
    });

活动功能代码如下: -

function active(value,value2,value3)
    {


        $("#"+value).css({'display':'block'});
        $("#area"+value3+"_link").css({'text-decoration':'underline'});
        $('#'+value2).mouseout(function(){$('#'+value).css({'display':'none'});$("#area"+value3+"_link").css({'color':'#707070','text-decoration':'none'});});
    }

并且没有显示js错误。

4 个答案:

答案 0 :(得分:2)

为什么使用$(this).attr('onmouseover'。我看到的唯一原因是i

您只需使用.index()

即可
$("#map area").on('mouseover',  function(){
    var i = $("#map area").index(this) + 1;
    active('area'+ i, $(this).attr('id'), i);
})

注意:.index()0

开头

答案 1 :(得分:0)

尝试更改为匿名函数定义,如下所示

$(document).ready(function(){

         var i = 1; 
        $("#map area").each(function(){
            var Id = $(this).attr('id');
            $(this).attr('onmouseover', function() {...your code here...});
            i++;
// and you missed the ; after i++
        });
    });

答案 2 :(得分:0)

尝试:)

        $(document).ready(function(){
        var i = 1; 
        $("#map area").each(function(){
        var Id = $(this).attr('id');
        $(this).mouseover(function(){
        active('area"+i+"','"+Id+"',"+i+")");
        i++;
                             });

    });

答案 3 :(得分:0)

使用以下func附加事件,例如addEvent('mouseover', $(this).get(0), <callback>)

function addEvent(evnt, elem, func) {
if (elem.addEventListener)  // W3C DOM
  elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
  elem.attachEvent("on"+evnt, func);
}
else { // No much to do
  elem[evnt] = func;
}
}