我正在尝试创建一个跨浏览器动画侦听器,但该substr
功能有问题!
function animationEventListener(elem, listener, callback){
if(!elem){return;}
if(listener.length < 0){ console.error("Add event type listener"); }
listener = listener.toLowerCase();
var _prefix = ["", "o", "webkit", "MS"];
var _anmkeyword = new String("animation");
var _keywordlength = _anmkeyword.length;
var _nlistener;
for(var i = 0; i < 4; i++){
if(_prefix[i] == "MS" || _prefix[i] == "webkit"){
var _ol = _keywordlength + 1;
_nlistener = listener.substr(0, 1).toUpperCase()
+ listener.substr(1, listener.length - (listener.length - _keywordlength)-1)
+ listener.substr(9, 10).toUpperCase()
+ listener.substr(_keywordlength+1, listener.length);
}
console.log(_prefix[i] + listener + "\n" + _nlistener);
}
}
试试看:
listener = "animationend";
listener.substr(9, 10); // returns "end";
为什么?