javascript - document.anchors为什么不会返回<a href='#'>?
PHP中文网
PHP中文网 2017-04-11 09:49:37
[JavaScript讨论组]
<a name='xxx'>与<a href='xxx'>有什么区别吗?
<!DOCTYPE html>
<html>
<body>

<a name="html">HTML Tutorial</a><br>
<a name="css">CSS Tutorial</a><br>
<a name="xml">XML Tutorial</a>

<p>innerHTML of the first anchor:
<script>
document.write(document.anchors[0].innerHTML);
</script>
</p>

</body>
</html>
PHP中文网
PHP中文网

认证0级讲师

全部回复(2)
PHP中文网

官方说明:

For reasons of backwards compatibility, the returned set of anchors only contains those anchors created with the name attribute, not those created with the id attribute.

所以document.anchors只会返回有name属性的<a>节点。

另外,这种方式已经废弃,最好不要使用

This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.

天蓬老师

anchor是锚点,href是一个链接。
a标签带href是直接跳转到href的连接中去,a标签带name表示锚点,当点击的时候当前页面跳到锚点的位置,页面不刷新

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号