Javascript:通过鼠标悬停/鼠标悬停启用/禁用按钮
这应该非常简单:
如果我将鼠标光标放在此按钮上,它就会被禁用..耶! 但现在当我将光标移出时,它没有被启用......嘘。
我理解禁用它的概念意味着你不能用它做任何事情。但是如何才能在鼠标移出时启用它呢?是否可以?我错过了什么吗?
This should be pretty straightforward:
<input type="button" name="test" id="test" value="roll over me" onmouseover="this.disabled=true;" onmouseout="this.disabled=false;">
If I place the mouse cursor over this button, it gets disabled..yay!
But now when I move the cursor out, it doesn't get enabled...boo.
I understand the concept of disabling it means you can't do anything with it. But how do you get it to be enabled with a mouse out? Is it possible? Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该将 mouseout 设置为
disabled = ''
。禁用属性只会查看它是否存在。您可以设置disabled='anything',它将被禁用。过去,您只需要在属性中使用关键字
disabled
,但对于有效的 XHTML,您需要将每个属性设置为相同的值。编辑:
我对此进行了一些尝试,并向
SPAN
标记添加了一些填充,它允许事件正常工作。如果没有填充,它不会捕获事件,因为输入按钮被禁用。我只是将背景设为红色,这样就可以很容易地看到SPAN
用完的区域。You should set the mouseout to
disabled = ''
instead.The disabled property only looks to see if it's there at all. You can set disabled='anything' and it will be disabled. It used to be that you only needed the keyword
disabled
in your attributes but for valid XHTML you need to set every attribute equal to something.EDIT:
I played around with this a little bit and added some padding to the
SPAN
tag and it allows the events to work properly. Without padding, it's not trapping the events because the input button is disabled. I just made the background red so it was easy to see the area theSPAN
used up.潜水员来救援!
可惜没有人真正回答这个问题。这是可以做到的*。
然后:
Divs to the Rescue!
Too bad no one has really answered this question. It is possible to do*.
Then:
根据规范,禁用的表单元素不会触发鼠标事件。
我使用的解决方法是使用“禁用”类和与其关联的事件句柄来模拟禁用行为。适用于按钮,但我认为它不适用于文本输入和复选框。
Disabled form elements don't fire mouse events, by the spec.
What I use as a workaround is to simulate the disabled behavior with a 'disabled' class and event handles associate with it. Works well for buttons, but I suppose it wouldn't for text inputs and checkboxes.
您可以有一个围绕它的外部元素,并为该外部元素提供一个 onmouseover 来启用内部元素。
you could have an outer element that surrounds it and have an onmouseover for that outer element that enables the inner element.