Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.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_Html_Css - Fatal编程技术网

Javascript 按钮点击的两个功能

Javascript 按钮点击的两个功能,javascript,jquery,html,css,Javascript,Jquery,Html,Css,函数myFunction(){ document.getElementById(“tt”).style.marginTop=“50px”; document.getElementById(“tt”).style.marginTop=“80px”; } 找医生 您可以使用标志指示要执行的操作,并相应地将其上移或下移 var-switcherAtTop=true; var elem=document.getElementById('tt'); 元素添加列表器( “点击”, 函数(){ 如果(开关

函数myFunction(){
document.getElementById(“tt”).style.marginTop=“50px”;
document.getElementById(“tt”).style.marginTop=“80px”;
}

找医生

您可以使用标志指示要执行的操作,并相应地将其上移或下移

var-switcherAtTop=true;
var elem=document.getElementById('tt');
元素添加列表器(
“点击”,
函数(){
如果(开关顶部){
document.getElementById(“tt”).style.marginTop=“80px”;
switcherAtTop=false;
}否则{
document.getElementById(“tt”).style.marginTop=“50px”;
switcherAtTop=true;
}
}
)
.switcher{
边缘顶部:30px;
背景色:#430000;
宽度:300px;
高度:300px;
}

找医生
我是jQuery的粉丝

这是我的解决方案(通过使用2个类并切换它们):

$(“#快速搜索切换程序”)。单击(功能(){
$('.switcher').toggleClass('first').toggleClass('second');
});
。首先{
边缘顶部:60像素;
}
.第二{
边缘顶部:30px;
}

找医生

您可以一般地解决此问题,方法是将要替换的函数数组(在您的情况下为两个,但可以是任意数字)包装到一个函数中,该函数将调用新函数被调用次数的计数模数

这将适用于参数化函数,前提是您传递的所有函数都接受类似的参数

function alternate(arrayOfFunctions) {
    var counter = 0;
    return function() {
       var f = arrayOfFunctions[counter++ % arrayOfFunctions.length];
       return f.apply(this, arguments);     
    }
}

function doA() { console.log(doA.name);}
function doB() { console.log(doB.name);}
function doC() { console.log(doC.name);}

var newFunction = alternate([doA,doB,doC]);

newFunction();  // calls doA
newFunction();  // calls doB
newFunction();  // calls doC
newFunction();  // calls doA again, etc.

切换一个类…谷歌关于“切换类”,这很有帮助。