关于javascript中为firefox定义contain方法,这句while(obj!=null&&typeof(obj.tagName)!="undefind",疑问if(typeof(HTMLElement)!="undefined")//给firefox定义contains()方法,ie下不起作用\x09\x09{ \x09\x09 HTMLElement.prototype.contains=fu

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/05 17:02:44
关于javascript中为firefox定义contain方法,这句while(obj!=null&&typeof(obj.tagName)!=

关于javascript中为firefox定义contain方法,这句while(obj!=null&&typeof(obj.tagName)!="undefind",疑问if(typeof(HTMLElement)!="undefined")//给firefox定义contains()方法,ie下不起作用\x09\x09{ \x09\x09 HTMLElement.prototype.contains=fu
关于javascript中为firefox定义contain方法,这句while(obj!=null&&typeof(obj.tagName)!="undefind",疑问
if(typeof(HTMLElement)!="undefined")//给firefox定义contains()方法,ie下不起作用
\x09\x09{
\x09\x09 HTMLElement.prototype.contains=function (obj)
\x09\x09 {
\x09\x09\x09 while(obj!=null&&typeof(obj.tagName)!="undefind"){//
\x09  if(obj==this) return true;
\x09    \x09 obj=obj.parentNode;
\x09   \x09 }
\x09\x09\x09 return false;
\x09\x09 }
\x09}
以上是定义contain方法,while(obj!=null&&typeof(obj.tagName)!="undefind"不是判断obj是否存在么,为什么还要加typeof(obj.tagName)!="undefind",这个不多余么?

关于javascript中为firefox定义contain方法,这句while(obj!=null&&typeof(obj.tagName)!="undefind",疑问if(typeof(HTMLElement)!="undefined")//给firefox定义contains()方法,ie下不起作用\x09\x09{ \x09\x09 HTMLElement.prototype.contains=fu
在Firefox中节点间存在一个空节点(有可能是文本节点text-Node).空节点不具有节点名称,但却存在于文档的节点集合中.所以多数情况下判断tagName还是很重要的.具体情况你可以查看w3c中关于DOM 节点的解析.