【问题标题】:Call JQuery on load using Python CherryPy使用 Python CherryPy 在加载时调用 JQuery
【发布时间】:2015-05-13 09:32:22
【问题描述】:

我有以下 JQuery 函数,我想在页面加载时调用它。

class Fitbit(object):
@cherrypy.expose
def index(self):

    currentDate = (time.strftime("%d/%m/%Y"))

    return """<html>
    <head>
        <title>Fitbit</title>
        <link href="/static/css/fitbit.css" rel="stylesheet">
        <script>
           $(document).ready(function(){
               $('.bar-percentage[data-percentage]').each(function () {
          var progress = $(this);
          var percentage = Math.ceil($(this).attr('data-percentage'));
          $({countNum: 0}).animate({countNum: percentage}, {
            duration: 2000,
            easing:'linear',
            step: function() {
              // What todo on every count
            var pct = '';
            if(percentage == 0){
              pct = Math.floor(this.countNum) + '%';
            }else{
              pct = Math.floor(this.countNum+1) + '%';
            }
            progress.text(pct) && progress.siblings().children().css('width',pct);
            }
          });
        });
        });

        </script>
    </head>

    <body>

    <h4>{0}</h4>

                <article class="infoWindow">
                    <article class="infoLogo"><img alt="backDate" src="/static/images/footprint.png" width="40" height="40"/>Steps</article>
                    <div id="bar-1" class="bar-main-container azure">
                        <div class="wrap">
                            <div class="bar-percentage" data-percentage="38"></div>
                            <div class="bar-container">
                            <div class="bar"></div>
                        </div>
                    </div>
                  </div>
                </article>
    </body>

    </html>""" .format(currentDate)

    #return html
    index.exposed = True

if __name__ == '__main__':
 conf = {
 '/': {
     'tools.sessions.on': True,
     'tools.staticdir.root': os.path.abspath(os.getcwd())
 },
 '/static': {
     'tools.staticdir.on': True,
     'tools.staticdir.dir': './public'
 },
 '/images': {'tools.staticdir.on': True,
    'tools.staticdir.dir': './public'}
 }
 cherrypy.quickstart(Fitbit(), '/', conf)

这是在我的 HTML 中,在我的 .py 文件中使用 return """&lt;html&gt; 等。 我正在使用 CherryPy 来执行此操作,并且我需要找到在页面加载时如何调用此函数。我发现的所有示例都是在单击按钮时调用的,这对我没有任何帮助,特别是因为我是新手,不知道如何在加载时做类似的事情。谢谢

【问题讨论】:

    标签: javascript jquery python cherrypy


    【解决方案1】:

    这将在就绪时调用 init...

    $('document').ready(init);
            function init(){
                $('.bar-percentage[data-percentage]').each(function () {
                var progress = $(this);
                var percentage = Math.ceil($(this).attr('data-percentage'));
                $({countNum: 0}).animate({countNum: percentage}, {
                    duration: 2000,
                    easing:'linear',
                    step: function() {
                    // What todo on every count
                        var pct = '';
                        if(percentage == 0){
                            pct = Math.floor(this.countNum) + '%';
                        }else{
                            pct = Math.floor(this.countNum+1) + '%';
                        }
                    progress.text(pct) && progress.siblings().children().css('width',pct);
                    }
                });
            });
        };
    

    希望这会有所帮助!

    【讨论】:

    • 在上面添加了更多代码,以防我在运行上面时遇到关键错误时有点困惑
    • 当我运行它时仍然没有运行,只是空白的信息应该在哪里。
    • 啊破解了,我忘了'' 谢谢! :)
    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多