10

我正在使用 jQuery 使用 $.ajax(假设 test.html)通过 AJAX 加载页面。
它是一个简单的 HTML 文档,带有几个按钮和单击它们时的相关动画(也使用 jQuery)。
当我直接加载页面时,关联的 .click() 属性工作正常,但是当我在 AJAX 加载版本中单击它们时按钮无法响应。
由于我太累了,无法真正解释我正在做的所有闷闷不乐,我只是在下面编写所有代码,为此道歉。以下是文件中所需的代码。

<!-- p11.php -->
<!DOCTYPE html">
<html>
<head>
  <title>jQuery</title>
    <script type="text/javascript" src="c/js/jquery.js"></script>
    <script type="text/javascript" src="c/js/jtry11.js"></script>
    <link rel='stylesheet' type='text/css' href='c/css/css11.css'>
</head>
<body>
  <button id="go1">Load Something Using AJAX</button>
  <div id="msg"></div>
  <div id="showButton">
      <button id="showLoadedPage">Show the Page</button>
  </div>
  <div id="results"></div>
</body>
</html>

和下一个

$(document).ready(function()
{
    $('#results').hide();
    $('#msg').hide();
    $('#showButton').hide();
    $('#loading').hide();
    $('#loaded').hide();

    $('#load').live('click', function()
    {
        $('#load').hide();
        $('#loading').show();
        $('#msg').show('fast');
        $.ajax({
            url: 'test.html',
            cache: false,
            success: function(html) {
                    $('#results').append(html);
            }
        });
        $('#msg').ajaxSuccess(function(evt, request, settings){
           $(this).append('Click the Button Below to View the Page');
           $('#showButton').show('slow');
           $('#hideLoadedPage').hide();
           $('#loading').hide();
           $('#loaded').show();
        });
    });

    $('#showLoadedPage').live('click', function() {
        $('#loaded').hide('slow');
        $('#msg').hide('slow');
        $('#results').show('fast');
        $('.shower').hide();
        $(this).hide();
        $('#hideLoadedPage').show();
    });

    $('#hideLoadedPage').live('click', function() {
        $('#results').hide('fast');
        $('.shower').hide();
        $(this).hide();
        $('#showLoadedPage').show();
    });

    $('.hider').live('click', function() {
        $('.shower').show();
        $(this).hide();
        $('p.one').hide('slow');
        $('p.two').hide('medium');
        $('p.three').hide('fast');
    });

    $('.shower').live('click', function() {
        $('.hider').show();
        $(this).hide();
        $('p.one').show('slow');
        $('p.two').show('medium');
        $('p.three').show('fast');
    });

    $('p.*').live('click', function() {
        $(this).hide('slow');
        if( $('p').is(':hidden') ) {
            $('.shower').show();
        }
        if( $('p.*').is(':hidden') ) {
            $('.hider').show();
        }
    });
});

和最后一个

<!-- test.html -->
Page Loaded by Ajax.
Not the original Page

<center>
    <button class="hider">
        <img src="c/images/zoom_out.png" alt="zoom_out" />
        Hide 'em
    </button>
    <button class="shower">
        <img src="c/images/zoom_in.png" alt="zoom_out" />
        Show it
    </button>
    <p class="one">Hiya</p>
    <p class="two">Such interesting text, eh?</p>
    <p class="three">The third Paragraph</p>
</center>

我希望我没有犯什么大错?

4

3 回答 3

26

听起来你需要

$('#go1').live('click', function() {});

$.fn.live,因为事件处理程序仅在初始 dom 创建时注册,并且您正在重新填充 DOM 并且那些未附加。

更多信息@http ://docs.jquery.com/Events/live

于 2009-09-14T02:23:31.103 回答
1
 $("#peopleResult_list").on('click','a', function(event){
    //do you code here  
 });

在您的页面中当前 dom 的事件工作以及将来由 ajax 加载的任何 dom

于 2012-08-31T09:10:14.467 回答
1

如果我没看错,您将在开头添加点击处理程序。这些仅影响当前按钮。您可能只需要将其更改为现场活动。

于 2009-09-14T02:24:03.210 回答