释放鼠标按钮时,并不总是触发onmouseup [英] onmouseup isn't always triggered when mouse button is released

查看:90
本文介绍了释放鼠标按钮时,并不总是触发onmouseup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.frostjedi.com/terra /scrip...onmouseup.html


在Firefox中,如果我点击黑框然后发布,我会得到一个

弹出窗口。如果我点击,拖动光标,然后释放,我不会。

同样,如果我点击然后在黑匣子外面释放,我会b / b
don没有弹出窗口,我不明白为什么。


如果我注释掉document.getElemenById(" demo)。onmousedown =

mousedown;,它有效。为什么会有所作为呢?


这里是代码:


<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0 Strict // EN"

" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

< html xmlns =" http://www.w3.org/1999/xhtml" XML:朗= QUOT;恩" >

< head>

< title> onmousedown / onmouseup< / title>

< / head>


< body id =" body">

< div style =" background:black;宽度:50px;身高:50px id =" demo">< /

div>

< script type =" text / javascript">

document.body.onmouseup = mouseup;

document.getElemenById(" demo")。onmousedown = mousedown;


function mouseup(){

alert(test);

}


function mousedown(){}

< / script>

< / body>

< / html>

解决方案

yawnmoth写道:


如果我注释掉document.getElemenById(" demo)。onmousedown =

mousedown;",有用。为什么会有所作为?



仔细看看它。如果您需要线索:


如果我注释掉document.getElemenById(" demo)。onmousedown =



______________________________________ ^ ___________ ___


t


yawnmoth写道:


[...]

如果我注释掉document.getElemenById(" demo)。onmousedown =

mousedown;",有用。为什么会有所作为?



您忘记了t。


这里是代码:


<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0 Strict // EN"

" http://www.w3.org /TR/xhtml1/DTD/xhtml1-strict.dtd">

< html xmlns =" http://www.w3.org/1999/xhtml" XML:朗= QUOT;恩" >

< head>

< title> onmousedown / onmouseup< / title>

< / head>


< body id =" body">



ID是不必要的。可能有人之前告诉过你。


< div style =" background:black;宽度:50px;身高:50px ID = QUOT;演示">



您还应该定义前景色,并在`#demo'选择器的

CSS规则中声明所有内容。 />


< script type =" text / javascript">

document.body.onmouseup = mouseup;

document.getElemenById(" demo")。onmousedown = mousedown;



你不应该混合使用专有和标准兼容的功能(

至少没有功能测试)。


函数mouseup(){

alert(test);

}


function mousedown(){}



现在广泛支持函数表达式,在这里使用函数声明不需要
。尤其不是当你使用W3C DOM时,用户代理没有实现AFAIK,只有

支持1.2版之前的JavaScript版本。

PointedEars < br $>
-

现实主义:HTML 4.01严格

福音:XHTML 1.0严格

疯狂:XHTML 1.1作为应用/ xhtml + xml

- Bjoern Hoehrmann


8月26日上午11:48 *,Thomas''PointedEars''Lahn< PointedE ... @ web.de>

写道:


yawnmothwrote:


[...]

如果我注释掉document.getElemenById(" demo)。onmousedown =

mousedown;",它可以工作。 *为什么会有所作为?



你忘记了t。



是的 - 感谢你和Stevo指出这一点!


这个ID是不必要的。 *之前可能有人告诉过你。



有人有。请注意,我实际上并没有使用

getElementById调用来引用它。我只是复制/粘贴了HTML(例如,我没有记住

DOCTYPE),我猜有一个冗余我不知道
。有点像你没有使用任何身份证明 - 如果你注意到它们就可以删除


< div style =" background:black;宽度:50px;身高:50px ID = QUOT;演示">



您还应该定义前景色,并在`#demo''选择器的

CSS规则中声明所有内容。



我认为对于一个真正的现场制作网站来说,这绝对是一个很好的想法。 CSS的主要内容是将内容与美学分开 -

来实现它,这样你就不必编辑HTML来改变布局。


但是,对于像这样的快速小事,我不确定为什么它会产生重要影响?如果你有一个样式标签,你甚至可以将

#demo的CSS规则拆分成多个组,这会让它变得有点困难

让某人看到你的东西这样做,即使它可能更多

简洁......


< script type =" text / javascript">

document.body.onmouseup = mouseup;

document.getElemenById(" demo")。onmousedown = mousedown;



您不应该混合使用专有和标准兼容的功能(至少在没有功能测试的情况下,不能用于b $ b)。



就此而言,即使是符合标准的代码,功能测试似乎也值得一试

?仅仅因为某人的浏览器

支持JavaScript1.3并不意味着有人不能使用仅支持JavaScript1.2的

浏览器。
< blockquote class =post_quotes>


function mouseup(){

* alert(" test");

}


function mousedown(){}



现在广泛支持函数表达式,它没有出现

这里使用函数声明。 *尤其不是当你使用W3C DOM时,用户代理没有实现AFAIK,只有

支持1.2版之前的JavaScript版本。



我必须牢记这一点......谢谢!


http://www.frostjedi.com/terra/scrip...onmouseup.html

In Firefox, if I click in the black box and then release, I get a
popup. If I click, drag the cursor, and then release, I don''t.
Similarly, if I click and then release outside of the black box, I
don''t get a popup, and I don''t understand why.

If I comment out "document.getElemenById("demo").onmousedown =
mousedown;", it works. Why would that make a difference?

Here''s the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>onmousedown / onmouseup</title>
</head>

<body id="body">
<div style="background: black; width: 50px; height: 50px" id="demo"></
div>
<script type="text/javascript">
document.body.onmouseup = mouseup;
document.getElemenById("demo").onmousedown = mousedown;

function mouseup() {
alert("test");
}

function mousedown() {}
</script>
</body>
</html>

解决方案

yawnmoth wrote:

If I comment out "document.getElemenById("demo").onmousedown =
mousedown;", it works. Why would that make a difference?

Look very closely at it. If you need a clue:

If I comment out "document.getElemenById("demo").onmousedown =

______________________________________^___________ ___

t


yawnmoth wrote:

[...]
If I comment out "document.getElemenById("demo").onmousedown =
mousedown;", it works. Why would that make a difference?

Your forgot a "t".

Here''s the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>onmousedown / onmouseup</title>
</head>

<body id="body">

The ID is unnecessary. Probably someone has told you that before.

<div style="background: black; width: 50px; height: 50px" id="demo">

You should also define the foreground color, and declare everything in the
CSS rule for the `#demo'' selector.

<script type="text/javascript">
document.body.onmouseup = mouseup;
document.getElemenById("demo").onmousedown = mousedown;

You should not be mixing proprietary and standards-compliant features (at
least not without feature test).

function mouseup() {
alert("test");
}

function mousedown() {}

Function expressions are widely supported nowadays, it does not appear
necessary to use function declarations here. Especially not when you
use the W3C DOM which AFAIK was not implemented by user agents that only
supported JavaScript versions before 1.2.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


On Aug 26, 11:48*am, Thomas ''PointedEars'' Lahn <PointedE...@web.de>
wrote:

yawnmothwrote:

[...]
If I comment out "document.getElemenById("demo").onmousedown =
mousedown;", it works. *Why would that make a difference?


Your forgot a "t".

Yup - thanks to both you and Stevo for pointing that out!

The ID is unnecessary. *Probably someone has told you that before.

Someone has. Note that I didn''t actually reference it with a
getElementById call. I just copy / pasted the HTML (I don''t have the
DOCTYPE memorized, for instance) and I guess there was a redundancy I
was unaware of. Kinda like having any id that you don''t use - they
could all be deleted if you noticed them.

<div style="background: black; width: 50px; height: 50px" id="demo">


You should also define the foreground color, and declare everything in the
CSS rule for the `#demo'' selector.

I think for a real live production website that''s definitely a good
idea. The main point of CSS is to separate content from aesthetics -
to make it so you don''t have to edit the HTML to alter the layout.

For a quick little thing like this, though, I''m not sure why it''d
matter? If you had a style tag you could even split the CSS rules for
#demo up into multiple groups which would make it a bit more difficult
for someone to see what you''re doing, even though it might be more
concise...

<script type="text/javascript">
document.body.onmouseup = mouseup;
document.getElemenById("demo").onmousedown = mousedown;


You should not be mixing proprietary and standards-compliant features (at
least not without feature test).

For that matter, it seems like feature tests are probably worth while
for even standards-compliant code? Just because someones browser
supports JavaScript1.3 doesn''t mean someone can''t come along with a
browser only supporting JavaScript1.2.

function mouseup() {
* alert("test");
}

function mousedown() {}


Function expressions are widely supported nowadays, it does not appear
necessary to use function declarations here. *Especially not when you
use the W3C DOM which AFAIK was not implemented by user agents that only
supported JavaScript versions before 1.2.

I''ll have to keep that in mind... thanks!


这篇关于释放鼠标按钮时,并不总是触发onmouseup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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