為什么給多個對象就不能實現(xiàn)鏈式運動了?用1個運動還是可以的。
var?oProduct?=?document.getElementById('product'); var?oImg?=?oProduct.getElementsByTagName('li'); for(var?i=0,l=oImg.length;i<l;i++){ /*?給每個對象都加個定時器timer?*/ oImg[i].timer?=?null; oImg[i].onmouseover=function(){ startMove(this,'opacity',100,function(){ startMove(oImg[i],'height',150,function(){ startMove(oImg[i],'width',250); }); }); } oImg[i].onmouseout?=?function(){ startMove(this,'width',200,function(){ startMove(oImg[i],'height',200,function(){ startMove(oImg[i],'opacity',30); }); }); } }
2016-07-21
內(nèi)部函數(shù)里面的this對象指的是window對象。你可以在window.onload外面定義一個window屬性
var i =?'這個i是window的屬性';
然后把你貼出的這部分代碼改成
olmg[i].i ?=?'這個i是oImg[i]的屬性';
oImg[i].onmouseover = function () {
????????????alert(this.i);
? ? ? ? ? ? startMove(_this, 'opacity', 100, function () {
????????????????????alert(this.i);
? ? ? ? ? ? ? ? });
? ? ? ? }
你就可以看到這兩個this分別指的什么,也就是說鑲套函數(shù)里面已經(jīng)不能夠再用this代表olmg[i],只能在這個函數(shù)執(zhí)行的時候把olmg[i]賦給一個變量,我這里定義一個that變量,就保存了調(diào)用這個函數(shù)的olmg[i]的引用
oImg[i].onmouseover = function () {
? ? ? ? ? ??var that = this;
? ? ? ? ? ? startMove(that, 'opacity', 100, function () {
? ? ? ? ? ? ? ? startMove(that, 'height', 150, function () {
? ? ? ? ? ? ? ? ? ? startMove(that, 'width', 250);
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? }
2016-06-06
oImg[i].onmouseover = function () {
? ? ? ? ? ??var _this = this;
? ? ? ? ? ? startMove(_this, 'opacity', 100, function () {
? ? ? ? ? ? ? ? startMove(_this, 'height', 150, function () {
? ? ? ? ? ? ? ? ? ? startMove(_this, 'width', 250);
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? }
改成這樣,即可,你那種,嵌套里面的fn是找不到對應的對象的,具體可以在方法里,打印obj.nodename,就能看到嵌套里的startMove,沒有找到對象