博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

HTML 注解

Posted on 2011-10-20 17:26  PHP-张工  阅读(646)  评论(0编辑  收藏  举报

代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
</head>
<body>
<!--haha&#13;ha--><!sllsdfjalsdfjasdf>
<span id="span1" title="<!--haha&#10;&#13;h\r\na-->">sss</span>
<script type="text/javascript">
alert(document.getElementsByTagName(
'*').length); //所有元素个数,IE包含注解
if(document.getElementsByTagName('!').length > 0)
{
alert(document.getElementsByTagName(
'!')[1].data);
}
alert(document.documentElement.outerHTML);
//获取HTML的源码
alert(document.getElementById('span1').title);//innerHTML包含注解,innerText不包含注解
</script>
</body>
</html>

结论

HTML注解 <!--haha&#13;ha--><!sllsdfjalsdfjasdf> 均可

使用 document.getElementsByTagName('*') 获取所有网页元素时,IE(IE9非兼容模式不包含)包含注解。

IE中获取注解的方法 document.getElementsByTagName('!')

在 <span id="span1" title="<!--haha&#10;&#13;h\r\na-->">sss</span> 中不算注解 在title中回车可用 &#10;&#13;

获取当前网页源码 document.documentElement.outerHTML