在 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/