当不同的元素有不同的属性时,可以用如下方法
每个函数都有一个prototype属性,这个属性是指向一个对象的引用,这个对象称为原型对象,原型对象包含函数实例共享的方法和属性,也就是说将函数用作构造函数调用(使用new操作符调用)的时候,新创建的对象会从原型对象上继承属性和方法。
$(document).ready(function(e) {
var oUl = $("#scrollList");var oUl2 = $("#scrollList2" );
function Test1(o){
this.i = 0;
var that = this;
this.iTimer = setTimeout(function(){
that.change_pic(o);
},2000);
}
Test1.prototype = {
change_pic:function(o){
clearTimeout(this.iTimer);
var ul_li= o.children();
var len=ul_li.length;
if(this.i==len-1){
this.i=-1;
}
ul_li.eq(++this.i).addClass("on");
var that = this;
this.iTimer = setTimeout(function(){
that.change_pic(o);
},2000);
}
}
//必须使用new 方法
new Test1(oUl2);