如何使Javascript不覆盖以前的函数?
谢谢大家的回答 您需要使用局部变量,以便每个函数都有独立于其他函数的变量。例如,更改此选项:如何使Javascript不覆盖以前的函数?,javascript,Javascript,谢谢大家的回答 您需要使用局部变量,以便每个函数都有独立于其他函数的变量。例如,更改此选项: $("#overEar ul li a").ready(function(){ var thisBlock = $("#heroBlock"); var maxWidth = 358; /*controls max width of the block*/ var minWidth = 180; var thisBlock = $("#reverbBlock"); /*cont
$("#overEar ul li a").ready(function(){
var thisBlock = $("#heroBlock");
var maxWidth = 358; /*controls max width of the block*/
var minWidth = 180;
var thisBlock = $("#reverbBlock"); /*controls min widht of the block*/
var maxWidth = 340;
var minWidth = 180;
var thisBlock = $("#solosBlock");
var maxWidth = 402;
var minWidth = 180;
$("#overEar ul li a").hover(
function(){
$(thisBlock).animate({width: minWidth+"px"}, { queue:false, duration:500 });
$(this).animate({width: maxWidth+"px"}, {specialEasing:'easeInOutBounce', duration:300}, { queue:false, duration:500 });
$(this).animate({width: maxWidth-"px"}, {specialEasing:'easeInOutBounce', duration:100}, { queue:false, duration:500 });
thisBlock = this;
}
);
})
为此:
thisBlock = $("#heroBlock");
maxWidth = 400; /*controls max width of the block*/
minWidth = 100; /*controls min widht of the block*/
var
关键字将变量限制为声明变量的特定函数。您需要使用局部变量,以便每个函数都有独立于其他函数的变量。例如,更改此选项:
$("#overEar ul li a").ready(function(){
var thisBlock = $("#heroBlock");
var maxWidth = 358; /*controls max width of the block*/
var minWidth = 180;
var thisBlock = $("#reverbBlock"); /*controls min widht of the block*/
var maxWidth = 340;
var minWidth = 180;
var thisBlock = $("#solosBlock");
var maxWidth = 402;
var minWidth = 180;
$("#overEar ul li a").hover(
function(){
$(thisBlock).animate({width: minWidth+"px"}, { queue:false, duration:500 });
$(this).animate({width: maxWidth+"px"}, {specialEasing:'easeInOutBounce', duration:300}, { queue:false, duration:500 });
$(this).animate({width: maxWidth-"px"}, {specialEasing:'easeInOutBounce', duration:100}, { queue:false, duration:500 });
thisBlock = this;
}
);
})
为此:
thisBlock = $("#heroBlock");
maxWidth = 400; /*controls max width of the block*/
minWidth = 100; /*controls min widht of the block*/
var
关键字将变量限制为声明变量的特定函数。Ruakh,非常感谢您的回答。现在可以了。但是,我有另一个问题出现了。ruakh,我在原始问题下发布了另一个问题,你能看一下吗?非常感谢您的时间。@YifanChen:您发布了实际代码了吗?这对我来说毫无意义;您有一个单独的函数,可以在其中设置thisBlock
和maxWidth
和minWidth
,然后在使用它们之前,立即用thisBlock
和maxWidth
和minWidth
的不同值覆盖它们。这是实际代码的一部分。对这就是我的问题所在,我不希望最后一个值一直覆盖前一个值,即thisBLock的值。因此,我对thisBlock的不同值进行了签名,比如var thisBlock=$(“heroBlock”)/$(“reverbBlock”)/$(“solousBlock”),并给出了不同的minWidth和maxWidth。然而,不知怎么的,它不起作用。@YifanChen:我想你是编程新手吧?我建议你找一本书或教程学习;这将比在不真正理解脚本的情况下通过组装脚本来学习零碎的内容更容易,也更有效-/鲁克,非常感谢你的回答。现在可以了。但是,我有另一个问题出现了。ruakh,我在原始问题下发布了另一个问题,你能看一下吗?非常感谢您的时间。@YifanChen:您发布了实际代码了吗?这对我来说毫无意义;您有一个单独的函数,可以在其中设置thisBlock
和maxWidth
和minWidth
,然后在使用它们之前,立即用thisBlock
和maxWidth
和minWidth
的不同值覆盖它们。这是实际代码的一部分。对这就是我的问题所在,我不希望最后一个值一直覆盖前一个值,即thisBLock的值。因此,我对thisBlock的不同值进行了签名,比如var thisBlock=$(“heroBlock”)/$(“reverbBlock”)/$(“solousBlock”),并给出了不同的minWidth和maxWidth。然而,不知怎么的,它不起作用。@YifanChen:我想你是编程新手吧?我建议你找一本书或教程学习;这将比在不真正理解脚本的情况下通过组装脚本来学习零碎的内容更容易,也更有效-/