码迷,mamicode.com
首页 > 其他好文 > 详细

个性A标签

时间:2014-08-08 01:50:25      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   问题   div   代码   

问题:

前段时间,小琳同学问我A标签为啥alert出来的是它的href?

示例:

先来两个标签比较一下。

<a id="a" href="http://www.baidu.com">baidu.com</a>
<span id="b">baidu.com</span>

再敲点代码比较一下。

alert(a);              // "http://www.baidu.com/"        不是常见的啥啥对象
alert(b);              // "[object HTMLSpanElement]"     正常的啥啥对象
alert(a.id);           // "a"                            能取到标签的属性
alert(a.href);         // "http://www.baidu.com/"
alert(a==a.href);      // true                           表示结果相同
alert(a===a.href);     // false                          表示本质不同
alert(a.toString());   // "http://www.baidu.com/"        A标签toString返回的是它的href

可以发现,问题出在A标签的toString上,再比较一下。

alert(a.toString==b.toString);                // false
alert(b.toString==document.body.toString);    // true

这表示A标签的toString函数不是继承来的,而是被重新了,它返回自己的href值。

收获:

个性标签,还有没有了?

 

个性A标签,布布扣,bubuko.com

个性A标签

标签:style   blog   http   color   os   问题   div   代码   

原文地址:http://www.cnblogs.com/ccforeverd/p/3898369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!