【问题标题】:javascript function doesn't work after load加载后javascript函数不起作用
【发布时间】:2015-01-18 06:29:36
【问题描述】:

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

     <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>

【问题讨论】:

  • 动态插入元素等

标签: jquery ajax delegates load


【解决方案1】:

由于您使用的是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>

【讨论】:

  • 感谢您的回答。但它仍然不起作用。但这对我了解这里发生的事情有很大帮助。我按照你说的做了,还是不行。我也尝试过使用 AjaxComplete。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
相关资源
最近更新 更多