[英]Sending a variable to another function
I have these 2 functions and would like 1 to execute the other with a specified variable:我有这 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');
};
As you might guess, this does not work.正如您可能猜到的那样,这不起作用。 Question is why?
问题是为什么? Could you give me a quick course on variable handling?
你能给我一个关于变量处理的快速课程吗? Thanks guys, I know you are the best!
谢谢大家,我知道你们是最棒的!
The simplest option is to define your function to take a parameter:最简单的选择是定义您的 function 以获取参数:
function ckbmovestate(element) {
$(element).css("-webkit-transform", "translate(53px, 0px)")
}
You can then call this with ckbmovestate(this)
from your code.然后,您可以使用代码中的
ckbmovestate(this)
调用它。
If you really want to be able to use the this
keyword within a function when it's called, you can do so.如果您真的希望能够在调用 function 时使用
this
关键字,您可以这样做。 It's not worth it here, but there may come a time when you want to do this.在这里不值得,但可能会有一段时间你想这样做。 To achieve this, you need to make use of
call
or apply
.为此,您需要使用
call
或apply
。 Here, either would work:在这里,任何一个都可以工作:
ckbmovestate.call(someElement);
This sends the value of someElement
to ckbmovestate
.这会将
someElement
的值发送到ckbmovestate
。 Inside ckbmovestate
, you will be able to access the value using the keyword this
.在
ckbmovestate
中,您将能够使用关键字this
访问该值。 So in you code above, the following call would work:因此,在您上面的代码中,以下调用将起作用:
ckbmovestate.call(this);
This is almost certainly overcomplicating things for this situation, however -- much easier to define a parameter.但是,对于这种情况,这几乎肯定会使事情变得过于复杂——定义参数要容易得多。
because you don't accept an argument in ckbmovestate因为您不接受 ckbmovestate 中的参数
your ckbmovestate function should look like this if you don't want to change too much of your code:如果您不想更改太多代码,您的 ckbmovestate function 应该如下所示:
function ckbmovestate(element){
element.css("-webkit-transform", "translate(53px, 0px)");
}
If you often use the ckbmovestate function, I suggest you make your own little plugin of it, which you do like this;如果你经常使用ckbmovestate function,我建议你自己做一个小插件,你这样做;
$.fn.ckbmovestate = function(){
$(this).each(function(){
$(this).css("-webkit-transform", "translate(53px, 0px)");
});
}
than, in the jquery each loop in your code in the question, you can write this:比,在 jquery 问题中代码中的每个循环中,您可以这样写:
$('fieldset:has(:checkbox:checked)').each(function(){
$(this).find('.switch').attr('data-state', 'on')
.ckbmovestate();
});
Change the function to use a parameter.更改 function 以使用参数。
function ckbmovestate( x ) {
x.css("-webkit-transform", "translate(53px, 0px)")
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.