onmousedown - 向左还是向右?

发布于 2025-01-07 08:08:43 字数 290 浏览 0 评论 0原文

首先,我不是在寻找 jQuery 解决方案,只是在元素内部寻找简单的纯 Javascript 代码。

假设我们有以下 html 代码:

<select onmousedown=" ??? ">...</select>

我想要元素内部有一个简单的脚本来显示弹出消息 alert() ,其中包含按下了哪个按钮以及元素与文档的相对位置的信息 - 类似 jQuery 中的 offset()

First of all, I am not looking for jQuery solution, just simple pure Javascript code, inside of an element.

Let's say we have following html code:

<select onmousedown=" ??? ">...</select>

I want a simple script inside of the element to show popup message alert() with information which button was pushed down and what is a relative position of the element to the document <body> - something like offset() in jQuery.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花海 2025-01-14 08:08:43

创建一个具有某个名称的 JavaScript 函数,然后在 onmousedown 事件上调用它,传递可在函数内部使用的 eventthis 对象。

HTML

<select onmousedown="onMouseDown(event, this)">...</select>

JS

function onMouseDown(e, obj){
   e = e || window.event; //window.event for IE

   alert("Keycode of key pressed: " + (e.keyCode || e.which));
   alert("Offset-X = " + obj.offsetLeft);
   alert("Offset-Y = " + obj.offsetTop);

}

如果您打算使用 jQuery,那么您可以使用此脚本

$('select').mousedown(function(e){
    alert("Keycode of key pressed: " + e.which);

    //Inside the handler this points to the select DOM element
    alert("Offset-X = " + $(this).offset().left);
    alert("Offset-Y = " + $(this).offset().top); 
});

更新:

如果您想要内联脚本,请尝试此操作。

<select onmousedown="function(e, obj){ e = e || window.event;alert('Keycode of key pressed: ' + (e.keyCode || e.which));alert('Offset-X = ' + obj.offsetLeft);alert('Offset-Y = ' + obj.offsetTop);}(event, this);">...</select>

Create a JavaScript function with some name and then call it on onmousedown event passing the event and this object which can be used inside the function.

HTML

<select onmousedown="onMouseDown(event, this)">...</select>

JS

function onMouseDown(e, obj){
   e = e || window.event; //window.event for IE

   alert("Keycode of key pressed: " + (e.keyCode || e.which));
   alert("Offset-X = " + obj.offsetLeft);
   alert("Offset-Y = " + obj.offsetTop);

}

If you plan to use jQuery then you can use this script

$('select').mousedown(function(e){
    alert("Keycode of key pressed: " + e.which);

    //Inside the handler this points to the select DOM element
    alert("Offset-X = " + $(this).offset().left);
    alert("Offset-Y = " + $(this).offset().top); 
});

Update:

If you want inline script then try this.

<select onmousedown="function(e, obj){ e = e || window.event;alert('Keycode of key pressed: ' + (e.keyCode || e.which));alert('Offset-X = ' + obj.offsetLeft);alert('Offset-Y = ' + obj.offsetTop);}(event, this);">...</select>
陈年往事 2025-01-14 08:08:43

MouseEvent.button在不同浏览器中有不同的值

MouseEvent.button == 1// means left key in ie6~ie8
MouseEvent.button == 0// means left key in ie9&others

MouseEvent.button has different values in different browsers

MouseEvent.button == 1// means left key in ie6~ie8
MouseEvent.button == 0// means left key in ie9&others
东京女 2025-01-14 08:08:43

window.captureEvents(Event.MOUSEDOWN)
window.onmousedown = mouseDown

function mouseDown(e)
{
  xPos = e.screenX;
  yPos = e.screenY;
  alert('onmousedown foo ' + ' x:' + xPos + ' y:'+ yPos);
}

编辑

<select id="foo" onmousedown="mouseDown()">...</select>

window.captureEvents(Event.MOUSEDOWN)
window.onmousedown = mouseDown

function mouseDown(e)
{
  xPos = e.screenX;
  yPos = e.screenY;
  alert('onmousedown foo ' + ' x:' + xPos + ' y:'+ yPos);
}

Edit

<select id="foo" onmousedown="function mouseDown(e){alert(MouseEvent.button + ' x:' + e.screenX + ' y:'+ e.screenY);}">...</select>

紫竹語嫣☆ 2025-01-14 08:08:43

编辑

在继续之前,您可能想阅读这篇文章:Javascript 疯狂:鼠标事件

单击文档会发送 MouseEvent 对象 具有许多属性 - 例如 MouseEvent.button 告诉您按下了哪个鼠标按钮,MouseEvent.ctrlKey告诉您单击发生时是否按下了控制键。

请注意,这些按钮并不是真正的“左”和“右”,因为它们可以根据用户偏好进行更改,您想知道的是它是否是主按钮(通常是左按钮,但可能是右按钮,或者可能是完全不同的按钮)来自其他定点设备)或辅助按钮(通常是右侧的,但同样可以是任何东西)。

一些播放代码:

<script>
function clickDetails(e) {
  e = e || window.event;
  var msg = [];

  for (var p in e) {
    msg.push(p + ': ' + e[p]);
  }
  document.getElementById('msg').innerHTML = msg.join('<br>');
}
    
window.onload = function() {
  document.getElementById('sp0').onmousedown = clickDetails;
}

</script>

<div>
  <span id="sp0">click me</span>
  <br>
  <span id="msg"></span>
</div>

编辑

啊,到底是什么:

<select onmousedown="alert('Button: ' + event.button
                         + '\nPosition: ' + this.offsetLeft 
                         + ',' + this.offsetTop);">

Edit

You might like to read this article before you go any further: Javascript Madness: Mouse Events

Clicks on a document dispatch a MouseEvent object that has lots of properties—e.g. MouseEvent.button tells you which mouse button was pressed, MouseEvent.ctrlKey tells you if the control key was pressed when the click occured.

Note that the buttons aren't really "left" and "right", since they can be changed by user preferences, what you want to know is whether it was the primary button (usually left but might be right or could be something totally different from some other pointing device) or the secondary button (usually right but again, could be anything).

Some play code:

<script>
function clickDetails(e) {
  e = e || window.event;
  var msg = [];

  for (var p in e) {
    msg.push(p + ': ' + e[p]);
  }
  document.getElementById('msg').innerHTML = msg.join('<br>');
}
    
window.onload = function() {
  document.getElementById('sp0').onmousedown = clickDetails;
}

</script>

<div>
  <span id="sp0">click me</span>
  <br>
  <span id="msg"></span>
</div>

Edit

Ah, what the hell:

<select onmousedown="alert('Button: ' + event.button
                         + '\nPosition: ' + this.offsetLeft 
                         + ',' + this.offsetTop);">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文