在事件上中断函数

Break function on event

本文关键字:函数 中断 事件      更新时间:2023-09-26

我有以下功能:

li.click(function () {
    div = 0;
    $(this).children('label').children('div').on('click', function () {
        return div = 1;
    });
    console.log(div);
    if (div === 1) {
        console.log(123);
        return;
    }

我想退出函数,当"div"被点击,但div变量总是为0。如何改变这种状况?

当点击ul中的label中的li中的div时,将variable div设置为1,并在该值为1或不为1时驱动特殊行为。这里的工作提琴:http://jsfiddle.net/9pxgdpgz/

Javascript:

var div = 0;
$("li").on("click", function () {
    if (div === 1){
        alert("div is 1");
        div = 0;
        console.log(123);
        return;
    } else {
        alert("div is not 1");   
    }
    // Do more stuff here if not 1?
});
$("li").children("label").children("div").on("click", function () {
    // Set global variable to 1.
    div = 1;
});
HTML:

<ul>
<li>
    LI IS HERE<br />
    <label>
        LABEL IS HERE<br />
        <div>
            DIV IS HERE
        </div>
    </label>
</li>
</ul>