【问题标题】:jQuery Re-run script on ajax loaded contentjQuery在ajax加载的内容上重新运行脚本
【发布时间】:2013-09-25 05:26:25
【问题描述】:

我正在使用 wordpress 的 Advanced Ajax Page Loader 插件通过 ajax 加载内容,我的自定义 jquery 脚本在通过 ajax 加载时没有在内容上运行时遇到问题。如果我直接从它们的 url 加载所述页面,脚本运行良好,但通过 ajax 加载导航到它们会导致它们无法工作。

我正在使用的示例脚本是

var o = {
    init: function(){
        this.diagram();
    },
    random: function(l, u){
        return Math.floor((Math.random()*(u-l+1))+l);
    },
    diagram: function(){
        var r = Raphael('diagram', 600, 600),
            rad = 73,
            defaultText = 'Skills',
            speed = 250;

        r.circle(300, 300, 85).attr({ stroke: 'none', fill: 'rgba(0,0,0,.10)' });

        var title = r.text(300, 300, defaultText).attr({
            font: '14px ralewaylight',
            fill: '#fff'
        }).toFront();

        r.customAttributes.arc = function(value, color, rad){
            var v = 3.6*value,
                alpha = v == 360 ? 359.99 : v,
                random = o.random(91, 100),
                a = (random-alpha) * Math.PI/180,
                b = random * Math.PI/180,
                sx = 300 + rad * Math.cos(b),
                sy = 300 - rad * Math.sin(b),
                x = 300 + rad * Math.cos(a),
                y = 300 - rad * Math.sin(a),
                path = [['M', sx, sy], ['A', rad, rad, 0, +(alpha > 180), 1, x, y]];
            return { path: path, stroke: color }
        }

        jQuery('.get').find('.arc').each(function(i){
            var t = jQuery(this), 
                color = t.find('.color').val(),
                value = t.find('.percent').val(),
                text = t.find('.text').text();

            rad += 30;  
            var z = r.path().attr({ arc: [value, color, rad], 'stroke-width': 20 });

            z.mouseover(function(){
                this.animate({ 'stroke-width': 30, opacity: 1 }, 1000, 'elastic');
                if(Raphael.type != 'VML') //solves IE problem
                this.toFront();
                title.stop().animate({ opacity: 0 }, speed, '>', function(){
                    this.attr({ text: text + '\n' + value + '%' }).animate({ opacity: 1 }, speed, '<');
                });
            }).mouseout(function(){
                this.stop().animate({ 'stroke-width': 20, opacity: 1 }, speed*4, 'elastic');
                title.stop().animate({ opacity: 0 }, speed, '>', function(){
                    title.attr({ text: defaultText }).animate({ opacity: 1 }, speed, '<');
                }); 
            });
        });

    }
}
jQuery(function(){ o.init(); });

我知道您可以将 .delegate 用于点击功能,但这对非点击功能没有帮助。

附注:该插件带有“重新加载代码”功能,我粘贴的任何脚本在控制台中都显示为重新加载但实际上不起作用。

【问题讨论】:

  • 尝试用“.live”替换“.on”
  • @Afreen - 这是一个糟糕的建议。 on 不久前替换了 live(现已弃用)

标签: javascript php jquery ajax wordpress


【解决方案1】:

试试这个:$(document).on('click', 'a[href^="#"]', function...

【讨论】:

  • 我特别需要非点击功能的解决方案
  • 什么是“非点击功能”?写任何例子。
  • 看来你应该在通过ajax加载页面后运行o.init();
猜你喜欢
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 2012-09-03
  • 1970-01-01
  • 2017-03-15
  • 1970-01-01
相关资源
最近更新 更多