2015-11-17 35 views
0

的读取属性“getTotalLength”我有此SVG文字动画http://jsfiddle.net/thLvLkq0/2/SVG动画 - 错误不能为空

function init(i) { 
    path[i] = document.getElementById('path'+i); 
    length = path[i].getTotalLength(); 
    path[i].style.transition = path[i].style.WebkitTransition = 'none'; 

    length[i] = length; 
    path[i].style.strokeDasharray = length + ' ' + length; 
    path[i].style.strokeDashoffset = length; 

    path[i].getBoundingClientRect(); 
} 

Chrome浏览器返回该错误消息Cannot read property 'getTotalLength' of null

有什么办法解决呢?我无法让它工作。

回答

2

你得到这个是因为它正在寻找一个ID为path3的元素,但是你只有path1path2。您可以通过将for(var i=1; i<=3; i++)更改为for(var i=1; i<3; i++)来停止for循环。

+0

是的,你是对的!非常感谢你! – DMande

+1

console.log是你的朋友:) –