javascript - 将 URL 分配给 a.href 的神奇之处

标签 javascript html

在 Chrome 开发者控制台上,我输入了以下代码:

> a = document.createElement('a');
> a.href = "http://localhost:4200/setting?q=123&b=aa";

然后该元素的其他属性也拥有相应的值,例如:

> a.search
"?q=123&b=aa"
> a.hostname
"localhost"
> a.port
"4200"

我对结果感到困惑。我刚刚将 url 分配给 href 属性。
为什么其他属性可以同时拥有它们的值?
这一现象背后有何魔力?
谁能帮我解释一下吗?

最佳答案

What's the magic behind the phenomenon?

没有“魔法”,但简单的规范

https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement :

[HTMLAnchorElement] inherits properties from its parent, HTMLElement, and implements those from HTMLHyperlinkElementUtils.

The HTMLHyperlinkElementUtils mixin defines utility methods and properties to work with HTMLAnchorElement and HTMLAreaElement. These utilities allow to deal with common features like URLs.

关于javascript - 将 URL 分配给 a.href 的神奇之处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47280538/

相关文章:

javascript - UIWebView 中出现奇怪的 WebKit 错误 - 这可以被捕获吗?

javascript - 三个 JS 着色器库 - 多纹理

javascript - 如何将 DOM 对象绑定(bind)到 JS 对象

html - 在固定的 div 大小内安装带有文本的图像

html - 什么是 z-index 的正确使用

javascript - 使用 jQuery mobile 从右/左滑动单个 div?

Javascript滑动元素性能

javascript - 如何理解 JavaScript 同步/异步

javascript - 如何重新提交 HTML 表单

html - CSS 选择器 - 确定从最后一个 child 开始的偶数/奇数 child