gpt4 book ai didi

javascript - 尽管使用了 on(),但在将元素添加到 DOM 后,Jquery 单击事件不会触发

转载 作者:行者123 更新时间:2023-12-02 17:14:31 24 4
gpt4 key购买 nike

我想我一定在这里遗漏了一些明显的东西。

我有一个函数绑定(bind)到类上的单击事件。该函数将元素添加到 DOM 中的位置取决于单击的元素的位置。基本上,如果您单击多行框中的一个框,则会在单击的框所在行的下方添加一个信息显示 div(请参阅示例代码)。

但是,我实现此方法的方式似乎是,一旦添加了信息显示 div,单击事件就不再在添加的 div 之前出现在 DOM 中的任何元素上触发。它仍然会在添加 div 之后的元素上触发。

我已经通过 on() 委托(delegate)了事件处理程序,所以我不确定为什么它没有保持绑定(bind)。我错过了什么愚蠢的明显的事情吗?

预先感谢您抽出宝贵的时间。

<!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>

最佳答案

您要添加的 div 从屏幕顶部一直延伸到其位置,覆盖其上方的所有元素。

添加此 CSS 样式:

#cdisplay {
float: left;
}

关于javascript - 尽管使用了 on(),但在将元素添加到 DOM 后,Jquery 单击事件不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24550917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com