a:active vs mousedown事件preventDefault() - ed [英] a:active vs mousedown event preventDefault()-ed

查看:250
本文介绍了a:active vs mousedown事件preventDefault() - ed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个< a> 元素,其中


  • code>:active 在CSS中指定的状态

  • 我们在监听 mousedown 事件并由于以下原因调用 event.preventDefault() 结果是:在 Firefox中我将鼠标光标移动到锚点上并单击它 - 锚点不显示为:active

  • 我也测试过这个问题一些Chrome浏览器/ Opera / IE版本 - 所有的浏览器已经将锚点装饰得像:active



以下是一个测试案例:
http:// jsbin。 com / azajex / 4



$ b 在W3C上没有深入的描述,必须考虑 active 和when。



但是除FF以外的所有浏览器(可能不是全部?由CSS引擎渲染元素为:active 。这似乎是一个很长的时间问题,我在FF 3.6,8.0,13.0测试这个案例 - 所有的版本表现完全一样。



所以,在这种情况下浏览器的正确行为是什么? 解决方案

没有正确的行为。这取决于浏览器的实现,不应该依赖。这被称为未定义的行为。在软件工程中,不应该依赖未定义的行为,因为它没有真实的文档,结果也是不可验证的。

lockquote

CSS没有定义哪些元素可能处于上述状态[伪类::悬停,:活动和:焦点],或状态是如何进入和离开的。脚本可能会改变元素是否对用户事件做出反应,而不同的设备和UA [浏览器]可能有不同的方式指向或激活元素。
- W3C CSS选择器



The case

  • we have an <a> element with :active state specified in CSS
  • in JavaScript code we listen to mousedown event and call event.preventDefault() for some reasons :)
  • As a result: in Firefox when I move mouse cursor over the anchor and click it -- anchor is not displayed as :active
  • I have also tested this issue in some Chrome / Opera / IE versions -- all that browsers had decorated the anchor as if it was :active.

Here is a test for the case: http://jsbin.com/azajex/4

The question

On W3C there is not deep description, what must be considered active and when.

But all browsers (maybe, not all?) except FF handle mousedown firstly by CSS engine and render the element as :active. It appears to be a long time issue, I tested this case in FF 3.6, 8.0, 13.0 -- all that versions behave exactly the same.

So, what is the right behaviour of a browser in this case?

解决方案

There is no 'correct behavior'. It is up to browser implementations and should not be relied upon. This is known as undefined behavior. In software engineering, undefined behaviors should be not be relied upon as it has no true documentation nor are the results verifiable.

CSS does not define which elements may be in the above states [pseudo-classes: :hover, :active, and :focus], or how the states are entered and left. Scripting may change whether elements react to user events or not, and different devices and UAs[browsers] may have different ways of pointing to, or activating elements. -W3C CSS Selectors

这篇关于a:active vs mousedown事件preventDefault() - ed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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