Django jQuery不工作? [英] Django jQuery not working?

查看:73
本文介绍了Django jQuery不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模板样式:

{% extends 'typer/base.html' %}
{% load url from future %}

{% load staticfiles %}

{% block title %}{{ p_name }}{% endblock %}

{% block body_block %}
<script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script>
    $('#user_passage_text').on('keyup', function(e) {
        if ( e.which === 13 ) {
        var time = (new Date()).getTime() - $(this).data('time');
        $(this).data('time', 0);
        console.log('Time passed : ' + time + ' milliseconds');

        }else if ( !$(this).data('time') ){
            $(this).data('time', (new Date()).getTime());
        }
    });
</script>

    <h1>{{ p_name }}</h1>
    <p>This passage is {{ p_wlength }} words, {{ p_clength }} characters long.</p>
    {% if passage %}
        <p><blockquote>{{ p_text_body|linebreaksbr }}</blockquote></p>

    {% else %}
        The specified text {{ p_name }} does not exist!
    {% endif %}

    <p>
        Begin typing to start the test.
    </p>
    <textarea id="user_passage_text"></textarea>


{% endblock %}

这样做是打印一个消息到控制台来检查该功能是否正常工作。我不明白的原因是我可以在 http://jsfiddle.net/d8c4z300/ 完全正常那么为什么在我运行django模板的时候打印消息呢?

All I want for this to do is print a message to the console to check that the function is working. The reason I'm confused about it not working is that I can run it on http://jsfiddle.net/d8c4z300/ perfectly fine. So why does the message print there but not when I run the django template?

推荐答案

你需要 $ (function(){...})(或 $(document).ready(function(){...}); 等于,只有更长)你的js代码:

you need $(function(){ ... }) (or $( document ).ready(function() { ... }); which is equal, only longer) around your js code:

so:

$(function(){

  $('#user_passage_text').on('keyup', function(e) {
    if ( e.which === 13 ) {
    var time = (new Date()).getTime() - $(this).data('time');
    $(this).data('time', 0);
    console.log('Time passed : ' + time + ' milliseconds');

    }else if ( !$(this).data('time') ){
        $(this).data('time', (new Date()).getTime());
    }
  });

});

小提琴正在工作,因为您设置为 onload 几乎等于 $(function(){...})这里。

fiddle is working because you set it to onload which is almost equal to $(function(){ ... }) here.

这篇关于Django jQuery不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆