jQuery变量与此
我试图为我正在重用的代码(activechange)应用一个变量,但我做错了什么。有没有更好的方法来定义这些。我也不知道如何通过创建自己的函数,将.fadeIn()“…”代码添加为变量jQuery变量与此,jquery,variables,Jquery,Variables,我试图为我正在重用的代码(activechange)应用一个变量,但我做错了什么。有没有更好的方法来定义这些。我也不知道如何通过创建自己的函数,将.fadeIn()“…”代码添加为变量 $(function () { var activechange = $(this).addClass('active').siblings('.active').removeClass('active'); $('#video-left').mouseover(function () {
$(function () {
var activechange = $(this).addClass('active').siblings('.active').removeClass('active');
$('#video-left').mouseover(function () {
activechange;
$('#product-left').fadeIn().addClass('active').siblings('.active').fadeOut().removeClass('active');
});
$('#video-center').mouseover(function () {
activechange;
$('#product-center').fadeIn().addClass('active').siblings('.active').fadeOut().removeClass('active');
});
$('#video-right').mouseover(function () {
activechange;
$('#product-right').fadeIn().addClass('active').siblings('.active').fadeOut().removeClass('active');
});
});
将其包装为函数:
function blah(elem) {
elem.addClass('active').siblings('.active').removeClass('active');
}
$('#video-left').mouseover( function(){
blah($(this))
});
将代码包装到函数中
var activechange = function (){
$(this).addClass('active').siblings('.active').removeClass('active');
}
用法
或
用法
您试图将变量应用于什么?我不知道你在问什么。我只是有重复的代码,我想分配给一个变量,但它不适合我。(activechange变量)您可能也希望提供IIFE示例,因为OP的函数就是这种形式。
activechange.apply(this);
var activechange = function (obj){
$(obj).addClass('active').siblings('.active').removeClass('active');
}
activechange(this);