您的位置:程序门 -> web 开发 -> javascript



[javascript]“种类不一致”错误


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


[javascript]“种类不一致”错误[已结贴,结贴人:fitzwilliam]
发表于:2007-07-24 18:49:23 楼主
能够正常运行,但是总是页面总是有错误:   种类不一致。
没有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   an   array   of   dom   nodes
for   (   var   j   =   0;   j   <   a[i].length;   j++   )
r[r.length]   =   a[i][j];
}   else   {   //   otherwise,   assume   it 's   a   dom   node
r[r.length]   =   a[i];
}
}
return   r;
}


//--> </script>

</head>
<body   id= "body ">
<h1   id= "h1 "> introduction   to   the   dom </h1>
<ol>

</ol>

<input   id= "input1 "   name= "submit "   type= "submit "     style   =   "   width   :   100     "class= " "   value= "test "   onclick= "test(); ">
</body>
</html>
发表于:2007-07-24 20:24:271楼 得分:50
for   (   var   i   =   0;   i   <   elems.length;   i++   )  

not

for   (   var   i   =   0;   i   <=   elems.length;   i++   )
发表于:2007-07-24 21:11:352楼 得分:25
同意
楼上正解
发表于:2007-07-25 09:27:483楼 得分:25
看来问题解决了。
发表于:2007-07-25 09:52:134楼 得分:0
恩,明白了。

3q。


快速检索

最新资讯
热门点击