单击一对元素时的事件

时间:2016-08-28 17:18:15

标签: javascript jquery jquery-events

我有5个元素,其中包含类.home和唯一ID'以及一个类.btn1的按钮。在.home之后单击.btn1时,反之亦然,应执行一个函数。我不知道我应该使用什么jQuery方法。

3 个答案:

答案 0 :(得分:2)

试试这个:

<!DOCTYPE html>
<html>
<head>
    <style>
        .active {
            background-color: skyblue;
        }
    </style>
</head>
    <body>
        <div class="home">I am Div1</div>
        <div class="home">I am Div2</div>
        <div class="home">I am Div3</div>
        <div class="home">I am Div4</div>
        <div class="home">I am Div5</div>
        <button class="btn">Run</button>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            var divReady = false;
            var btnReady = false;
            $(".btn").on("click",function(){
                btnReady = true;
                $(this).addClass("active");
                if (divReady)
                    fun();
            })
            $("div").on("click",function(){
                divReady = true;
                $("div").removeClass("active");
                $(this).addClass("active");
                if (btnReady)
                    fun();
            })
            
            function fun() {
                alert("I am working!")
                $(".btn,div").removeClass("active");
                btnReady = false;
                divReady = false;
            }
            
        })
    </script>
    </body>
</html>
        

答案 1 :(得分:0)

只需计算事件发生的次数,并存储事件。这可以通过数组轻松完成:

//create an array to store events
events=[];
//create a handler function
function handler(event){
//add the event to our array
events.push(event);
if(events.length==2){
//two events fired:
//first event:
a=events[0];
//second:
b=events[1];
//do whatever
}
}
//add an eventlistener that calls our handler and passes 'documentclick'   
 document.addEventListener("documentclick",function(){handler("click")});

答案 2 :(得分:0)

我认为没有特定的jQuery技巧。

为什么不使用类来存储状态?

&#13;
&#13;
        $(function() {

            $(".home").click(function () {
                $(this).addClass("active");
                if ($(".btn1").hasClass("active")) {
                    console.log("Doing something");
                }

            });


            $(".btn1").click(function () {
                        $(this).addClass("active");
                        if ($(".home").hasClass("active")) {
                            console.log("Doing something");
                        }
                    }
            );
        });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = 'home'>
        Home
    </div>
    <div>
        Stuff
    </div>
    <button class = 'btn1' >Button</button>
    <div class = 'home'>
        Home
    </div>
    <div>
        Stuff
    </div>
    <div class = 'home'>
        Home
    </div>
    <div>
        Stuff
    </div>
    <div class = 'home'>
        Home
    </div>
    <div>
        Stuff
    </div>
&#13;
&#13;
&#13;