简体   繁体   中英

Jquery click events not triggering after adding an element to the DOM, despite using on()

I think I must be missing something obvious here.

I have a function bound to the click event on a class. The function adds an element to the DOM in a location that depends on the position of the clicked element. Basically, if you click a box in several rows of boxes, then an information display div is added under the row of the clicked box (see example code).

However, the way I've implemented this it seems that once the information display div has been added, the click event no longer triggers on any of the elements appearing in the DOM before the added div. It will still trigger on elements after the added div.

I've delegated the event handler via on(), so I'm not sure why it's not staying bound. What dunderheaded obvious thing am I missing please?

Thanks in advance for your valuable time.

<!DOCTYPE html>
<html lang="en-gb">
<head>    

    <title>Box page</title>
    <link rel="stylesheet" type="text/css" media="" href="http://www.hud.ac.uk/media/universityofhuddersfield/styleassets/css/bootstrap.css" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script><!-- jquery.1.9.2.js -->    

<style>    

.cbox {
}    

.cbox h2 {
    margin: 5px;
    color: #fff;
}    

.green .squarepushed {
    background-color: #8CC63F;
}    

.pink .squarepushed {
    background-color: #EC008C;
}    

.blue .squarepushed {
    background-color: #00ADEF;
}    

.orange .squarepushed {
    background-color: #F89828;
}    

.squarepusher {
    margin-top: 100%;
}    

.squarepushed {
    position: absolute;
    top: 3px;
    bottom: 3px;
    left: 3px;
    right: 3px;
    border-radius: 5px;
    color: #fff;
}    

</style>    

<script>    

$(document).ready( function() {    

    $('#cboxes').on('click', '.cbox', function() {    

        console.log('Triggered!');    

        $('#cdisplay').remove();     

        var numcols = 6;
        var currpos = $(this).index()+1;
        var currrow = Math.ceil( currpos / numcols );
        var endsquare = currrow * numcols ;
        var html = "showing content for box "+currpos+" after box "+endsquare;
        $(this).parent().children('.cbox:nth-child('+endsquare+')').after("<div class='col-xs-12' id='cdisplay'><p>"+html+"</p></div>");    

    } );    

});    

</script>    

</head>
<body>    

<div class="container">
<div class="row" id="cboxes">    

<div class="cbox pink col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 1</h2>
    </div>
</div>    

<div class="cbox blue col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 2</h2>
    </div>    

</div>    

<div class="cbox blue col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 3</h2>
    </div>    

</div>    

<div class="cbox pink col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 4</h2>
    </div>    

</div>    

<div class="cbox pink col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 5</h2>
    </div>    

</div>    

<div class="cbox orange col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 6</h2>
    </div>    

</div>    

<div class="cbox green col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 7</h2>
    </div>    

</div>    

<div class="cbox orange col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 8</h2>
    </div>    

</div>    

<div class="cbox orange col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 9</h2>
    </div>    

</div>    

<div class="cbox orange col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 10</h2>
    </div>    

</div>    

<div class="cbox orange col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 11</h2>
    </div>    

</div>    

<div class="cbox green col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 12</h2>
    </div>
</div>    


<div class="cbox blue col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 13</h2>
    </div>    

</div>    

<div class="cbox pink col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 14</h2>
    </div>    

</div>    

<div class="cbox orange col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 15</h2>
    </div>    

</div>    

<div class="cbox orange col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 16</h2>
    </div>    

</div>    

<div class="cbox green col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 17</h2>
    </div>    

</div>    

<div class="cbox blue col-xs-2">    

    <div class="squarepusher"></div>
    <div class="squarepushed">
        <h2>Box 18</h2>
    </div>
</div>    



</div>
</div>    

</body>
</html>

The div you are adding stretches all the way from the top of the screen to it's position, covering all elements above it.

Add this css style:

#cdisplay {
    float: left;
}

Fast answer, Add this to css:

#cdisplay {
    z-index: -1;
}

The problem is when you append #cdisplay, it overshadows the #cboxes so you can not click on them anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM