日期:2014-05-16  浏览次数:20297 次

[JavaScript]“种类不一致”错误
能够正常运行,但是总是页面总是有错误:   种类不一致。
没有baidu到解决方法。
csdn上有篇类似的帖子,但似乎不是一回事。
哪位兄弟给看看下面的代码到底有什么问题。
谢谢!!

P.S.   程序可以直接运行。

<html   id= "html ">
<head   id= "head ">
<title   id= "title "> Introduction   to   the   DOM </title>

<script   id= "script ">

function   test(){
append(     document.getElementsByTagName( "ol ")[0],   " <li> Mouse   trap. </li> "   );

}

//Enhanced   Functions   for   Inserting   and   Appending   into   the   DOM
function   before(   parent,   before,   elem   )   {
//   Check   to   see   if   no   parent   node   was   provided
if   (   elem   ==   null   )   {
elem   =   before;
before   =   parent;
parent   =   before.parentNode;
}
//   Get   the   new   array   of   elements
var   elems   =   checkElem(   elem   );
//   Move   through   the   array   backwards,
//   because   we 're   prepending   elements
for   (   var   i   =   elems.length   -   1;   i   > =   0;   i--   )   {
parent.insertBefore(   elems[i],   before   );
}
}

function   append(   parent,   elem   )   {
//   Get   the   array   of   elements
var   elems   =   checkElem(   elem   );
//   Append   them   all   to   the   element
for   (   var   i   =   0;   i   <=   elems.length;   i++   )   {
parent.appendChild(   elems[i]   );
}
}

//Converting   an   Array   of   Mixed   DOM   Node/HTML   String   Arguments   into   a
//Pure   Array   of   DOM   Nodes
function   checkElem(a)   {
var   r   =   [];
//   Force   the   argument   into   an   array,   if   it   isn 't   already
if   (   a.constructor   !=   Array   )   a   =   [   a   ];
for   (   var   i   =   0;   i   <   a.length;   i++   )   {
//   If   there 's   a   String
if   (   a[i].constructor   ==   String   )   {
//   Create   a   temporary   element   to   house   the   HTML
var   div   =   document.createElement( "div ");
//   Inject   the   HTML,   to   convert   it   into   a   DOM   structure
div.innerHTML   =   a[i];
//   Extract   the   DOM   structure   back   out   of   the   temp   DIV
for   (   var   j   =   0;   j   <   div.childNodes.length;   j++   )
r[r.length]   =   div.childNodes[j];
}   else   if   (   a[i].length   )   {   //   If   it 's   an   array
//   Assume   that   it 's &n