相关疑难解决方法(0)

在jQuery中绑定动态创建的元素

以下代码是相当自我解释的,但我遇到了将click事件绑定到已创建的元素的问题.

你可以在第25行看到我正在创建一个id为'overlay'的div.然后我设置它的css属性.

然后在第65行我绑定点击以触发隐藏模态.问题是,它不起作用.如果我把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({ …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

3
推荐指数
1
解决办法
4502
查看次数

标签 统计

javascript ×1

jquery ×1