Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

jquery - How do I assign blank space in a div and the button within with the same functionality?

So I am trying to solve this problem with a program I am writing, I have a div, with the class .main. It occupies a certain amount of space. Within it, there are two buttons. When .button1 is clicked, .light1 is turned on. When .button2 is clicked, .light2 is turned on. BUT. I want it so that when .button1 OR the blank space in .main is clicked, light 1 turns on. So far, when I do that, only the empty space turns .light1 on. Removing .main from the function makes it so .button1 turns .light1 on.

The issue for me is, since the buttons are WITHIN the div, clicking any of buttons activates whatever would activate the div.

I have some working code for this, it is completely self contained, copy paste this into an HTML file if you like:

<!DOCTYPE html>

<head><meta charset="UTF-8">
    <title>I am a test page</title>

    <!--bootstrap 3.3.6 and jquery 2.23-->
    <script src="./js/jquery-2.2.3.min.js"></script>

    <script src="./js/bootstrap.min.js"></script>
    <link href="./css/bootstrap.min.css" rel="stylesheet"/>

    <style>
        .main
        {
        background-color: rgba(98,159,210, 0.3);
        border-radius: 1px;
        font-size: 14px;
        color: black;
        width: 400px;
        margin-left: auto;
        margin-right: auto;
        height: 50px;
        padding:3px;
        cursor: pointer;        
        }


        .button1
        {

        }

        .button2
        {

        }


        .light1
        {
            height: 100px;
            width: 100px;
            background-color: yellow;
        }

        .light2
        {
            height: 100px;
            width: 100px;
            background-color: red;   

        }
    </style>

</head>


<body>
<div class="container">
    <br><br>
    <div class="main">
         <button class="button1">button1</button>
         <button class="button2">button2</button>
    </div>
    <br>
    <br><br><br><br>
    <div class="light1"></div>
    <div class="light2"></div>

</div>
</body>

<script>
    $(document).on("click", ".button1, .main", function()
                   {
                    $(".light1").toggle();

                   });

    $(document).on("click", ".button2", function()
                   {

                    $(".light2").toggle();

                   });

</script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I got you correctly, following changes should be the answer:

$(document).on("click", ".button1, .main", function(e) {
    e.stopPropagation();

    $(".light1").toggle();

});

$(document).on("click", ".button2", function(e) {
    e.stopPropagation();

    $(".light2").toggle();

});

Read more about event.stopPropagation()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...