Django和jQuery的难度(为什么管理员应用程序中的$ undefined?) [英] Difficulty with Django and jQuery (why is $ undefined in the admin app?)

查看:105
本文介绍了Django和jQuery的难度(为什么管理员应用程序中的$ undefined?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我使用jQuery以来已经有一段时间了;我想我犯了一个愚蠢的错误。

It's been a while since I worked with jQuery; I think I'm making a stupid mistake.

这是一个Django小部件:

Here is a Django widget:

class FooWidget(Widget):

    # ...

    class Media:
        js = ('js/foowidget.js',)

这是.js文件:

alert("bar");

$(document).ready(function() {
  alert("omfg");
  $('.foo-widget').click(function() {
    alert("hi");
    return false;
  });
});

alert("foo");

唯一发生的 alert()是首先。我是否有一个愚蠢的语法错误?

The only alert() that fires is the first. Do I have a stupid syntax error or something?

此外,如果页面上的其他一些包含的脚本重新定义 $(document).ready ,我要担心吗?我猜测后续定义会覆盖我的,所以后续定义更安全,例如:

Also, if some other included script on the page redefines $(document).ready, do I have to worry about it? I'm guessing that subsequent definitions will override mine, so it's safer for subsequent definitions to do something like:

$(document).ready(function() {
    document.ready()
    // real stuff
});

jQuery已经包含在页面中<%c $ c> foowidget.js 是。

jQuery is already included in the page by the time foowidget.js is.

更新:根据@ lazerscience的链接,我尝试了以下方法,但它仍然像以前一样失败:

Update: Based on @lazerscience's link, I tried the following instead, but it still fails just as before:

alert("bar");

$(function() {
  alert("omfg");
  $(".set-widget").click(function() {
    alert("hi");
    return false;
  });
});

alert("foo");

更新2 :奇怪的是,当我这样做时:

Update 2: Curiously, when I do:

alert($);

我得到未定义。这表明jQuery实际上并未初始化。这让我感到困惑,因为< head> 包括:

I get "undefined". This suggests that jQuery is actually not initialized. This confuses me, because the <head> includes:

<script type="text/javascript" src="/media/js/jquery.min.js"></script>
<script type="text/javascript" src="/media/js/jquery.init.js"></script>
<script type="text/javascript" src="/media/js/actions.min.js"></script>
<script type="text/javascript" src="/static/js/foowidget.js"></script>

在jQuery脚本之后不放我的脚本确保 $

Doesn't putting my script after the jQuery scripts ensure that $ will be defined?

更新3 :来自jquery.min.js:

Update 3: from jquery.min.js:

/*!
 * jQuery JavaScript Library v1.4.2
 * http://jquery.com/
 *
 * Copyright 2010, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2010, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Sat Feb 13 22:33:48 2010 -0500
 */
(function(A,w){function ma(){if(!c.isReady){try{s.documentElement.doScroll("left")}catch(a){setTimeout(ma,1);return}c.ready()}}function Qa(a,b){b.src?c.ajax({url:b.src,async:false,dataType:"script"}):c.globalEval(b.text||b.textContent||b.innerHTML||"");b.parentNode&&b.parentNode.removeChild(b)}function X(a,b,d,f,e,j){var i=a.length;if(typeof b==="object"){for(var o in b)X(a,o,b[o],f,e,d);return a}if(d!==w){f=!j&&f&&c.isFunction(d);for(o=0;o<i;o++)e(a[o],b,f?d.call(a[o],o,e(a[o],b)):d,j);return a}return i? /* ... */

看起来对我来说是正确的。

Looks correct to me.

这是来自Firebug控制台:

This is from the Firebug console:

Error: $ is not a function
[Break On This Error] $(function() {
foowidget.js (line 5)
>>> $
anonymous()
>>> jQuery
undefined
>>> $('a')
null
>>> $(document)
null

更新4 $ 在我的Django网站的所有页面上定义,除了管理员应用程序。奇怪......

Update 4: $ is defined on all page of my Django site except the admin app. Odd...

更新5 :这很有趣。

jQuery.init.js

// Puts the included jQuery into our own namespace
var django = {
 "jQuery": jQuery.noConflict(true)
}; 

从控制台:

>>> $
anonymous()
>>> django
Object {}
>>> django.jQuery
function()
>>> django.jQuery('a')
[a /admin/p..._change/, a /admin/logout/, a ../../, a ../, a.addlink add/]


推荐答案

您可以使用 $(文件).ready() 尽可能多的频率在一个页面上。当你调用它时,它不会重新定义任何东西,但是 ready()的调用会添加在文档准备就绪时调用的函数。
调试代码,例如。使用Firebug,它会显示更多关于错误发生位置的详细信息!

You can use $(document).ready() as often as you want to on one page. It doesn't redefine anything as you call it, but the call of ready() adds functions that are called when "the document is ready". To debug your code eg. use Firebug that will show you more detailled information about where the error occurs if there's any!

这篇关于Django和jQuery的难度(为什么管理员应用程序中的$ undefined?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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