加载后javascript函数不起作用

时间:2015-01-18 06:29:36

标签: jquery ajax delegates load

我有一个很好的阅读更多脚本。见下文。但是,在我使用另一个带有加载的脚本之后,这个脚本不再工作了。

     <script>
$('#info').readmore({
  moreLink: '<a href="#">Usage, examples, and options</a>',
  collapsedHeight: 384,
  afterToggle: function(trigger, element, expanded) {
    if(! expanded) { // The "Close" link was clicked
      $('html, body').animate({scrollTop: $(element).offset().top}, {duration: 100});
    }
  }
});

$('artikel').readmore({speed: 500});
        </script>

这是我使用的代码,读取更多脚本不再起作用了:

    <script>

        $.ajaxSetup ({  
            cache: false  
        });

        var navItems = $(".noBounce").find("a"), 
            spinner = "<span class='loading'><img src='/media/ajax-loader.gif' width='15' alt='loading...' class='spinner'></span>";  


        navItems.click(function(e){
            e.preventDefault();
            navItems.removeClass("current");

            var $this = $(this),
                loc = $this.attr("href");
            $(".ajax-dump").html(spinner).load(loc);

            $this.addClass("current");
        });

    </script>

1 个答案:

答案 0 :(得分:0)

由于您使用的是jQuery,因此您应将代码包装在$(document).ready()

<script type="text/javascript">
    $(document).ready(function() {
        $('#info').readmore({
            moreLink: '<a href="#">Usage, examples, and options</a>',
            collapsedHeight: 384,
            afterToggle: function(trigger, element, expanded) {
                if (!expanded) { // The "Close" link was clicked
                    $('html, body').animate({
                        scrollTop: $(element).offset().top
                    }, {
                        duration: 100
                    });
                }
            }
        });

        $('artikel').readmore({
            speed: 500
        });
    });
</script>