可以使用 hash DOM 节点属性吗?
您是否建议不要阅读并使用 hash
DOM 节点属性? (您认为它将来可能会被弃用和删除吗?)
它是什么?对于链接 ,hash 属性的值为
#the-hash
。它似乎存在于所有浏览器中,请参阅 JavaScript 圣经第 603 页,并且我测试了 Safari、Chrome、 FF、IE 和 Opera。
背景:我使用 jQuery,但在 IE 7 中:$(elem).attr('href');
不会返回 href
属性,而是以服务器地址和页面路径为前缀的 href
。示例:如果 href
为 #the-hash
,则 attr
返回 http://server/folder/page#the-hash在 IE 7 中。 这里是一个jsfiddle示例。
Would you advise against reading and using the hash
DOM node property? (Do you think it might be deprecated and removed in the future?)
What is it? For a link <a href='http://server/folder/page#the-hash'>
, the value of the hash property is #the-hash
. It seems to be present in all browsers, see the JavaScript Bible page 603, and I've tested Safari, Chrome, FF and IE and Opera.
Background: I use jQuery, but in IE 7 this: $(elem).attr('href');
doesn't return the href
attribute, but rather the href
prefixed with server address and the path to the page. Example: if the href
is #the-hash
then attr
returns http://server/folder/page#the-hash
in IE 7.
Here is a jsfiddle example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您远离所有可能在遥远的将来某个时候被弃用的功能、API 等,那么您将永远无法完成任何事情。他们明天可能会弃用
alert
,或者for
...in
(可能性更大,但是呃。这不是重点。)担心什么现已弃用。而
location.hash
则不然。它是 HTML5 DOM 的成熟成员,并且在可预见的未来仍然如此。如果它最终被弃用,那么它可能会在很长一段时间内仍然受到支持,因为“生活标准”(据称 HTML 已成为这样)使得事物变得非常难以弃用和/或删除。无论哪种方式,如果您以后想更改它,都可以。
If you stay away from every feature, API, etc that might possibly be deprecated sometime in the distant future, you'll never get anything done. They could deprecate
alert
tomorrow, orfor
...in
(far more likely, but eh. that's beside the point.)Worry about what's deprecated now. And
location.hash
is not. It's a full-fledged member of the HTML5 DOM, and will be for the foreseeable future. If it ever does end up deprecated, it'll probably still be supported for ages after, as a "living standard" (which is what HTML has allegedly become) makes things exceedingly hard to deprecate and/or remove.Either way, if you want to change it later, you can.