Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将变量发送到另一个函数_Javascript_Jquery_Variables - Fatal编程技术网

Javascript 将变量发送到另一个函数

Javascript 将变量发送到另一个函数,javascript,jquery,variables,Javascript,Jquery,Variables,我有这2个函数,希望1使用指定变量执行另一个函数: function ckbmovestate() { $(this).css("-webkit-transform", "translate(53px, 0px)") }; function initcheckbox(){ var togglebox = "<div class='toggle_box'><div class='switch'></div></div>";

我有这2个函数,希望1使用指定变量执行另一个函数:

function ckbmovestate() {
    $(this).css("-webkit-transform", "translate(53px, 0px)")

    };

function initcheckbox(){
    var togglebox = "<div class='toggle_box'><div class='switch'></div></div>";
    $('input[type=checkbox]').css('display','none');
    $('fieldset[data-input-type=checkbox]').append(togglebox);
    $('fieldset:has(:checkbox:checked)').each(function(){
        $(this).find('.switch').attr('data-state', 'on');
        ckbmovestate($(this))
    });
    $('fieldset:has(:checkbox:checked)')
    $('fieldset:not(:has(:checkbox:checked))').find('.switch').attr('data-state', 'off');

};
函数ckbmovestate(){
$(this.css(“-webkit transform”,“translate(53px,0px)”)
};
函数initcheckbox(){
var-togglebox=“”;
$('input[type=checkbox]').css('display','none');
$('fieldset[data input type=checkbox]')。追加(切换框);
$('fieldset:has(:checkbox:checked)')。每个(函数(){
$(this.find('.switch').attr('data-state','on');
ckbmovestate($(此))
});
$('fieldset:has(:checkbox:checked'))
$('fieldset:not(:has(:checkbox:checked))).find('.switch').attr('data-state','off');
};

正如你可能猜到的,这是行不通的。问题是为什么?你能给我一个关于变量处理的快速课程吗?谢谢你们,我知道你们是最棒的

最简单的选项是定义函数以获取参数:

function ckbmovestate(element) {
    $(element).css("-webkit-transform", "translate(53px, 0px)")
}
然后,您可以使用
ckbmovestate(this)
从代码中调用它


如果您确实希望在调用函数时能够在函数中使用
this
关键字,您可以这样做。在这里不值得,但总有一天你会想这么做的。要实现这一点,您需要使用或。在这方面,两者都可以:

ckbmovestate.call(someElement);
ckbmovestate.call(this);
这会将
someElement
的值发送到
ckbmovestate
。在
ckbmovestate
中,您可以使用关键字
this
访问该值。因此,在上面的代码中,以下调用将起作用:

ckbmovestate.call(someElement);
ckbmovestate.call(this);

但是,在这种情况下,这几乎肯定会使事情变得过于复杂——定义参数要容易得多。

最简单的选择是定义函数以获取参数:

function ckbmovestate(element) {
    $(element).css("-webkit-transform", "translate(53px, 0px)")
}
function ckbmovestate( x ) {
    x.css("-webkit-transform", "translate(53px, 0px)")
};
然后,您可以使用
ckbmovestate(this)
从代码中调用它


如果您确实希望在调用函数时能够在函数中使用
this
关键字,您可以这样做。在这里不值得,但总有一天你会想这么做的。要实现这一点,您需要使用或。在这方面,两者都可以:

ckbmovestate.call(someElement);
ckbmovestate.call(this);
这会将
someElement
的值发送到
ckbmovestate
。在
ckbmovestate
中,您可以使用关键字
this
访问该值。因此,在上面的代码中,以下调用将起作用:

ckbmovestate.call(someElement);
ckbmovestate.call(this);

但是,在这种情况下,这几乎肯定会使事情变得过于复杂——定义参数要容易得多。

更改函数以使用参数

function ckbmovestate( x ) {
    x.css("-webkit-transform", "translate(53px, 0px)")
};

将函数更改为使用参数

function ckbmovestate( x ) {
    x.css("-webkit-transform", "translate(53px, 0px)")
};

因为您不接受ckbmovestate中的参数

如果不想更改太多代码,则ckbmovestate函数应如下所示:

function ckbmovestate(element){
   element.css("-webkit-transform", "translate(53px, 0px)");
}
如果你经常使用ckbmovestate函数,我建议你自己制作一个插件,你喜欢这样做

$.fn.ckbmovestate = function(){
$(this).each(function(){
   $(this).css("-webkit-transform", "translate(53px, 0px)");
});
}
然后,在问题代码中的jquery每个循环中,您可以编写以下代码:

 $('fieldset:has(:checkbox:checked)').each(function(){
        $(this).find('.switch').attr('data-state', 'on')
               .ckbmovestate();
    });

因为您不接受ckbmovestate中的参数

如果不想更改太多代码,则ckbmovestate函数应如下所示:

function ckbmovestate(element){
   element.css("-webkit-transform", "translate(53px, 0px)");
}
如果你经常使用ckbmovestate函数,我建议你自己制作一个插件,你喜欢这样做

$.fn.ckbmovestate = function(){
$(this).each(function(){
   $(this).css("-webkit-transform", "translate(53px, 0px)");
});
}
然后,在问题代码中的jquery每个循环中,您可以编写以下代码:

 $('fieldset:has(:checkbox:checked)').each(function(){
        $(this).find('.switch').attr('data-state', 'on')
               .ckbmovestate();
    });