jQuery无法在AJAX Loaded页面中工作

Ora*_*ind 10 javascript php ajax jquery click

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

而下一个

$(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();
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

和最后一个

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

我希望我没有犯很大的错误?

med*_*iev 26

听起来像你需要的

$('#go1').live('click', function() {});
Run Code Online (Sandbox Code Playgroud)

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

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

  • 我认为实时事件处理程序绑定到文档本身而不是特定元素,因此当事件对象在事件发生之后传递(如"click")时,它会检测单击了哪个元素,并巧妙地为各自的事件调用正确的事件处理程序事件/元素.您只应在使用元素重新填充DOM时使用它,例如在这种情况下,您不应该总是需要它. (3认同)