为什么在firefox中可以,但是在ie8中却显示'undefined undefined'? [英] why in firefox it is ok, but in ie8 it print 'undefined undefined'?

查看:94
本文介绍了为什么在firefox中可以,但是在ie8中却显示'undefined undefined'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
     <span onmouseover="tip(event,this);">程序错误<div class="content">good</div></span><br>
    </div>

<p id="vtip" style="position:absolute"><img id="vtipArrow" src="vtip_arrow.png" />testtest<span class="content"></span></p>

<script>

function tip(evt,s){
        $('p#vtip').show();

        xOffset = -10; // x distance from mouse
        yOffset = 10; // y distance from mouse 
        alert(evt.pageY+' '+evt.pageX)
}
</script>

在Firefox中还可以, 但是在ie8中,它会打印"undefined undefined"

in firefox it is ok, but in ie8 it print 'undefined undefined'

推荐答案

如前所述,如果要使用直接JS进行此操作,则对于大多数浏览器,必须使用pageY/pageX,对于IE,则必须使用clientX/clientY.

As noted, if you are going do this with straight JS, you'd have to use pageY/pageX for most browsers and clientX/clientY for IE.

由于您使用的是jQuery(我看到那里有$('p#vtip').show();),因此您也可以使用jQuery绑定事件.当您使用jQuery绑定事件而不是现在使用的内联事件时,jQuery还将规范您跨浏览器访问事件属性的方式.

Since you're using jQuery (I see you have $('p#vtip').show(); in there), you might as well use jQuery to bind the events, too. jQuery will also normalize the way you can access the event attributes across browsers when you use it to bind the events instead of the inline events you're using now.

请参见 jQuery事件文档.

这是一个如何以不同方式设置html的示例.给跨度一个类,并删除onmouseover.

Here's an example of how to set up the html differently. Give the spans a class and remove the onmouseover.

<span class='tipped'>程序错误<div class="content">good</div></span><br>

使用jQuery将mouseover事件分配给跨度.

Assign the mouseover event to the spans with jQuery.

<script>
  $('.tipped').mouseover(function(evt){
    $('p#vtip').show();

    xOffset = -10; // x distance from mouse
    yOffset = 10; // y distance from mouse 
    alert(evt.pageY+' '+evt.pageX)//this should work fine in IE too, since it's a jQuerified event object 
    });
</script>

这篇关于为什么在firefox中可以,但是在ie8中却显示'undefined undefined'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆