简体   繁体   English

Javascript onfocus不支持`event`作为参数?

[英]Javascript onfocus not supporting `event` as an argument?

I want to get current X and Y position of the cursor in the browser when I click or focus on some of the html elements. 当我单击或关注某些html元素时,我想获取浏览器中光标的当前XY位置。 When I do this with onclick it worked. 当我使用onclick执行此操作时。 But with onfocus it didn't. 但是有了onfocus却没有。 Please see the examples below.. 请参见下面的示例。

Working code 工作代码

<html>
<head>
    <title>Untitled</title>
    <script type="text/javascript">
<!--
function findXCoord(evt) {
    if (evt.x)  return evt.x; 
    if (evt.pageX) return evt.pageX; 
}

function findYCoord(evt) {
    if (evt.y) return evt.y; 
    if (evt.pageY) return evt.pageY; 
}
//-->
</script>
</head>

<body>
<a onclick="alert('The x coordinate is: ' + findXCoord(event) + ' and the y coordinate is: ' + findYCoord(event));">Hi</a>
</body>

But when I change onclick to onfocus , It didn't show the x,y positions 但是当我将onclick更改为onfocus时 ,它没有显示x,y位置

(onclick="alert.... to onfocus="alert.......) (onclick =“ alert ....到onfocus =” alert .......“)

I think you forget to place href tag, though it won't return you co-ordinate except ie but it will fire the event http://jsbin.com/irexog/edit#preview working solution. 我认为您忘记放置href标记了,尽管它不会返回您的坐标,除了ie之外,它会触发事件http://jsbin.com/irexog/edit#preview工作解决方案。

And ya onfocus is supported by the anchor. 锚点支持onfocus。

you need to modify the event handling specific to browser like this 您需要像这样修改特定于浏览器的事件处理

"onfocus" is not a mouse event, you can get mouse position only from mouse events. “ onfocus”不是鼠标事件,只能从鼠标事件获得鼠标位置。

You can use "onmouseover" if you really need it. 如果确实需要,可以使用“ onmouseover”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM