javascript - document.anchors为什么不会返回<a href='#'>?

查看:92
本文介绍了javascript - document.anchors为什么不会返回<a href='#'>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

解决方案

官方说明

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.

这篇关于javascript - document.anchors为什么不会返回&lt;a href=&#039;#&#039;&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆