biaoge's blog

以前端技术为主

《精通javascript》中的几个错误

with 4 comments

第86页,代码清单5-28中有这样一段:

//向后遍历数组,
//因为我们向前添加元素
for ( var i = elems.length - 1; i >= 0; i-- ) {
    parent.insertBefore( elems[i], before );
}

此处用一个反循环反而把被添加元素的顺序搞反了,用一个正循环才能得到正确的顺序。

另一个错误:在103页,表单事件那部分有这样一句:“select事件在<select>元素更新后触发”。事实上,<select>元素更新后只会触发onchange事件,不会触发onselect事件,参见这里:“Broadly speaking, buttons can generate click events, and text and select items can generate focus, blur, select, and change events. The one potentially confusing aspect of this organization of events is that selection lists cannot generate the select event. This is because they have no editable text. ”

Related Posts

Written by admin

November 16th, 2008 at 12:08 pm

Posted in 前端技术

4 Responses to '《精通javascript》中的几个错误'

Subscribe to comments with RSS or TrackBack to '《精通javascript》中的几个错误'.

  1. 还有一处错误:
    function parent(elem, num){
    num = num || 1;
    for(var i=0; i<num; i++){
    if(elem != null) elem = elem.parentNode;
    return elem;
    }

    }
    应为:
    function parent(elem, num){
    num = num || 1;
    for(var i=0; i<num; i++){
    if(elem != null) elem = elem.parentNode;
    }
    return elem;
    }
    要不然得不到正常的级别父级。

    豪情

    21 Jul 11 at 10:27 am

  2. 我看不出你写的这两段代码有任何区别啊,不是一样的吗?

    admin

    22 Jul 11 at 10:14 am

  3. 哦,我看出区别来了。
    书上是这么写的:
    function parent( elem, num ) {
    num = num || 1;
    for ( var i = 0; i < num; i++ )
    if ( elem != null ) elem = elem.parentNode;
    return elem;
    }
    for循环后面没有写括号,这个应该是没问题的,是你把括号写错了地方

    admin

    22 Jul 11 at 10:22 am

  4. 确实写错了。

    豪情

    22 Jul 11 at 11:10 pm

Leave a Reply