gpt4 book ai didi

javascript - 在 jQuery 中绑定(bind)动态创建的元素

转载 作者:行者123 更新时间:2023-11-29 10:00:17 25 4
gpt4 key购买 nike

以下代码很容易解释,但我遇到了将点击事件绑定(bind)到已创建元素的问题。

您可以在第 25 行看到我正在创建一个 id 为“overlay”的 div。然后我设置它的 css 属性。

然后在第 65 行,我绑定(bind)点击以触发模态窗口的隐藏。问题是,它不起作用。如果我将 div 放在 html 中,.click 会按预期工作。

感谢任何帮助。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Modal</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">

$(document).ready(function() {

// Select the link(s) with name=modal
$('a[name=modal]').click(function(e) {

// Cancel the link behavior
e.preventDefault();

// Get the id of this link's associated content box.
var id = $(this).attr('href');

// Find the screen height and width
var overlayHeight = $(document).height();
var overlayWidth = $(window).width();

// Create the overlay
$('body').append('<div id="overlay"></div>');

//Set css properties for newly created #overlay
$('#overlay').css({
'width' : overlayWidth,
'height' : overlayHeight,
'position':'absolute',
'left' : '0',
'top' : '0',
'z-index' : '9000',
'background-color' : '#000',
'display' : 'none'
});

// Get the viewpane height and width
var winH = $(window).height();
var winW = $(window).width();

// Center the modal
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);

// Transition effects for overlay
$('#overlay').fadeIn(1000).fadeTo(1000,0.8);

// Transition effect for modal
$(id).fadeIn(1000);

});

// Close link click = trigger out
$('.modal .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();

$('#overlay').fadeOut(1000);
$('.modal').fadeOut(200);
});

// Overlay click = trigger out
$('#overlay').click(function () {
$('#overlay').fadeOut(1000);
$('.modal').fadeOut(200);
});

// Load rules in to modal
$('#rules .text').load('rules.html');

});

</script>
<style type="text/css">

.modal{
position:absolute;
display:none;
z-index:9999;
}
#rules{
width:600px;
height:400px;
}
#rules p{
background: #fff;
margin: 0;
padding: 0;
}
#rules .head{
background: #ddd;
text-align: right;
padding: 0 10px;
height: 30px;
}
#rules .text{
height: 370px;
padding: 0 20px;
overflow: auto;
}

</style>
</head>
<body>

<p><a href="#rules" name="modal">Open Modal</a></p>

<div id="rules" class="modal">
<p class="head"><a href="#" class="close">close</a></p>
<p class="text"></p>
</div>

</body>
</html>

最佳答案

叠加层的点击事件在元素存在之前绑定(bind)。您需要使用 live binding保留绑定(bind)——否则每次创建元素时都必须进行绑定(bind)。只需将您的函数更改为使用 live(),如下所示:

$('#overlay').live('click', function () {
$('#overlay').fadeOut(1000);
$('.modal').fadeOut(200);
});

.modal .close 的点击事件在事件绑定(bind)时已经在 DOM 中定义。

普通事件绑定(bind)只考虑当前存在于 DOM 中的元素,而使用 live() 绑定(bind)的事件也适用于与选择器匹配的所有 future 元素。

关于javascript - 在 jQuery 中绑定(bind)动态创建的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2238616/

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