代码之家  ›  专栏  ›  技术社区  ›  S16

绑定jQuery中动态创建的元素

  •  3
  • S16  · 技术社区  · 15 年前

    下面的代码非常简单,但我遇到了一个问题,它将click事件绑定到已创建的元素。

    然后在第65行,我绑定click以触发模态的隐藏。问题是,它不起作用。如果我将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>
    
    5 回复  |  直到 15 年前
        1
  •  5
  •   Tatu Ulmanen    15 年前

    live binding 保留绑定—否则每次创建元素时都必须进行绑定。把你的函数改成 live() 这样地:

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

    .modal .close 当事件被绑定时,它的工作方式与DOM中已经定义的一样。

    普通事件绑定只考虑DOM中当前存在的元素,而事件绑定为 live()

        2
  •  0
  •   Jasper De Bruijn    15 年前
     // Overlay click = trigger out
        $('#overlay').click(function () {
            $('#overlay').fadeOut(1000);
            $('.modal').fadeOut(200);
        });
    

    $('a[name=modal]').click(function(e) { 部分,但在$ ('body').append('<div id="overlay"></div>'); 部分,它应该起作用。

        3
  •  0
  •   Sampson    15 年前

    如果以编程方式创建#覆盖,则需要将其与$.live()绑定;

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

    此绑定方法绑定到与提供的选择器匹配的所有当前和未来元素。

        4
  •  0
  •   sp.    15 年前

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

    您可能还应该将$('#overlay').fadeOut()更改为$(this).fadeOut()。

        5
  •  0
  •   Gabriele Petrioli    15 年前

    请记住,每次 a[name=modal]

    完成后从DOM中删除覆盖元素。。或者在click之外创建它,然后在click事件中显示/隐藏它。。

    因为你将在点击链接后创建。。 )