//获取ID
var $ = function (id) {return typeof id === "string" ? document.getElementById(id) : id};
 
//获取tagName
var $$ = function (tagName, oParent) {return (oParent || document).getElementsByTagName(tagName)};
 
//自动播放对象
var AutoPlay = function (id) {this.initialize(id)};
 
AutoPlay.prototype = {
initialize: function (id)
{
var oThis = this;
this.oBox = $(id);
this.oUl = $$("ul", this.oBox)[0];
this.aImg = $$("img", this.oBox);
this.timer = null;
this.autoTimer = null;
this.iNow = 0;
this.creatBtn();
this.aBtn = $$("li", this.oCount);
this.toggle();
this.autoTimer = setInterval(function ()
{
oThis.next()
}, 3000);
this.oBox.onmouseover = function ()
{
clearInterval(oThis.autoTimer)
};
this.oBox.onmouseout = function ()
{
oThis.autoTimer = setInterval(function ()
{
oThis.next()
}, 3000)
};
for (var i = 0; i < this.aBtn.length; i++)
{
this.aBtn[i].index = i;
this.aBtn[i].onmouseover = function ()
{
oThis.iNow = this.index;
oThis.toggle()
}
}
},
creatBtn: function ()
{
this.oCount = document.createElement("ul");
this.oFrag = document.createDocumentFragment();
this.oCount.className = "count";
for (var i = 0; i < this.aImg.length; i++)
{
var oLi = document.createElement("li");
oLi.innerHTML = i + 1;
this.oFrag.appendChild(oLi)
}
this.oCount.appendChild(this.oFrag);
this.oBox.appendChild(this.oCount)
},
toggle: function ()
{
for (var i = 0; i < this.aBtn.length; i++) this.aBtn[i].className = "";
this.aBtn[this.iNow].className = "current";
this.doMove(-(this.iNow * this.aImg[0].offsetHeight))
},
next: function ()
{
this.iNow++;
this.iNow == this.aBtn.length && (this.iNow = 0);
this.toggle()
},
doMove: function (iTarget)
{
var oThis = this;
clearInterval(oThis.timer);
oThis.timer = setInterval(function () 
{
var iSpeed = (iTarget - oThis.oUl.offsetTop) / 5;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
oThis.oUl.offsetTop == iTarget ? clearInterval(oThis.timer) : (oThis.oUl.style.top = oThis.oUl.offsetTop + iSpeed + "px")
}, 30)
}
};
window.onload = function()
{
new AutoPlay("slide");
};提示错误:
var $$ = function (tagName, oParent) {return (oParent || document).getElementsByTagName(tagName)};
uncaught typeerror:  object[object object] has no method 'getElementByTagName'求解~~

解决方案 »

  1.   

    你的方法写错了,应该是:getElementsByTagName你少写了一个s了。
      

  2.   

    var $$ = function (tagName, oParent) {return (oParent || document).getElementsByTagName(tagName)};
     有S啊
      

  3.   

    return (oParent || document).getElementsByTagName(tagName)多了两个括号改成这样       return oParent || document.getElementsByTagName(tagName)
      

  4.   

    document.getElementsByTagName(tagName)      这一句话,这样才是完整的。你确这样        document).getElementsByTagName(tagName)
      

  5.   

    var $$ = function (tagName, oParent) {
            var o=oParent||document;
            return o.getElementsByTagName(tagName)
        };
      

  6.   

    我把括号去掉了,结果
    this.oBox.appendChild(this.oCount);
    又提示uncaught typeerror: object[object object] has no method 'appendChild'
      

  7.   

    说明你这一句
    var $ = function (id) {return typeof id === "string" ? document.getElementById(id) : id};
    错了,和上一句没关系了 。你的意思是不是说,如果传进来的参数如果是string类型的,就返回document.getElementById(id)对象如果不是就还返回id
      

  8.   

    不应该  typeof id  这样写
    应该typeof(id)  这样写
      

  9.   


    确实,掌握的不太好啊~~还在努力中~~~不过按照这个方法改完了 还是
    this.oBox.appendChild(this.oCount);
    又提示uncaught typeerror: object[object object] has no method 'appendChild'
      

  10.   


    object[object object]
    你看看,你判断的时候,返回 的这个对象是什么东西?