根据视频里面的代码执行却显示为null http://www.imooc.com/video/7202

这个教程中的代码
  function g(selector){
    var method = selector.substr(0,1) == '.'? 'getElementsByClassName' : 'getElementById';
    return document[method](selector.substr(1));
}   
上面的g()在浏览器中测试没问题,在执行下面的rsort时
 function rsort(n){
     var photo_center = g('#photo_'+n);
     photo_center.className += ' photo_center ';
        
}    
讲师直接就执行了,我的浏览器却一直提示photo_center为null
请问下这是什么原因?

ff43:TypeError: photo_center is null
chrome:Uncaught TypeError: Cannot read property 'className' of null


qq_FATE刘琦_0
浏览 1957回答 1
1回答

伊望岁月

g('#photo_'+n);获取id为'photo_'+n的元素,你看看html中有没有这个元素
打开App,查看更多内容
随时随地看视频慕课网APP