HTML:
<a href='http://www.jsfiddle.net'><span>link</span></a>
脚本:
$('span').click(function(event) {
window.open('http://www.google.com');
event.stopImmediatePropagation();
//The line below does prevent jsfiddle.net from loading on the right.
//event.preventDefault();
});
$('a').click(function() {
//This function is not triggered as event propagation has been stopped.
alert('You will never see this.');
});
点击演示中的“链接”将打开 google.com 和 jsfiddle.net。我的问题来了:为什么 <a>
的默认行为是(在本例中打开 jsfiddle.net)由其子项继承(在本例中为 <span>
)?有什么规范可以引用吗?
提前致谢。
最佳答案
Why is the default behavior of
<a>
inherited by its children?
因为这是有道理的 :-) 每次点击链接或其后代都会触发一个事件,该事件的默认操作是激活链接。
Are there any specifications I can refer to?
检查 Default actions and cancelable events在 DOM 级别 3 规范中解释取消。但是,DOM3 本身并未指定任何默认操作。
这些是在 HTML 5 规范中完成的,特别是在 Interactive Content section 中的点击。 :
When a pointing device is clicked, the user agent must run these steps:
- [Set some flags]
- Let
e
be the nearest activatable element of the element designated by the user, if any. To obtain that, check whether the target has a defined activation behavior. If it does, take it, if it doesn't (and has a parent element) then repeat the checking with the parent element of the target.- If there is an element
e
, run pre-click activation steps for it if any.- Dispatch the required
click
event.- If there is an element
e
and the click event is not canceled, run post-click activation steps for it if any.- If there is an element
e
and the event is canceled, run canceled activation steps for it if any.- [Reset flags]
激活行为由元素本身定义,例如 <a>
element获取并跟踪超链接。
关于javascript - <a> (href) 的默认行为由其子项继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19601662/