javascript - <a> (href) 的默认行为由其子项继承?

标签 javascript jquery html dom

Demo on jsfiddle.net

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:

  1. [Set some flags]
  2. 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.
  3. If there is an element e, run pre-click activation steps for it if any.
  4. Dispatch the required click event.
  5. If there is an element e and the click event is not canceled, run post-click activation steps for it if any.
  6. If there is an element e and the event is canceled, run canceled activation steps for it if any.
  7. [Reset flags]

激活行为由元素本身定义,例如 <a> element获取并跟踪超链接。

关于javascript - <a> (href) 的默认行为由其子项继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19601662/

相关文章:

javascript - AJAX 请求在 HTTP_STREAM_PARSER_READ_HEADERS 之后被取消

Javascript slider 动态更改变量

javascript - jQuery 库中使用的设计模式

javascript - 拖动项目和 jquery 动画

javascript - 视频在设备中没有响应

Javascript - for循环仅打印最后一次迭代

javascript - 在 Laravel Blade 模板中使用 JS/JQuery 时搜索栏过滤器的问题

javascript - 悬停时我可以处理其他元素吗?

使用 jQuery 输入表单的 javascript 效果

html - 中心幻灯片文本 html