如何更改$(“#button”)。one(“click”,first); to $(“#button”)。one(“click”,second);点击按钮

时间:2016-03-11 12:43:44

标签: javascript

index.html:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1-10-1.js"></script>
<script type="text/javascript" src="jquery-ui-1-9-2.js"></script>
<script>

$(document).ready(function(){
    $("#button1").click(function(){
        $("#div").css("background","red");

        //some code here

    });
});
$(function (){
    function first(){
        $("#div").css("background","red");
        $(this).one("click",second);
    }
    function second(){
        $("#div").css("background","blue");
        $(this).one("click",first);
    }
    $("#button2").one("click",first);
});

</script>

</head>
<body>
<input type="button" id="button1" value="button1"></input>
<input type="button" id="button2" value="button2"></input>
<div id="div">some text</div>
</body>
</html>

当我点击button1时,我应该将一些代码放入

$(“#button2”)。one(“click”,first); 变为 $(“#button2”)。one(“click”,second); < /强>

1 个答案:

答案 0 :(得分:1)

您需要unbind现有的one点击事件并设置新的one

$("#button2").unbind("click");  
$("#button2").one("click",second);

这应该解开第一个并绑定第二个。

$("#button1").click(function(){
    $("#div").css("background","red");
    $("#button2").unbind("click");  
    $("#button2").one("click",second);
});