jQuery未定义(在[Symfony]模板环境中) [英] jQuery is not defined (in [Symfony] templating environment)

查看:65
本文介绍了jQuery未定义(在[Symfony]模板环境中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示一个使用jQuery的页面.该页面中实现的AJAX功能不起作用.

I am trying to display a page that uses jQuery. The AJAX functionality implemented in the page does not work.

我正在使用FF进行调试.当我在控制台面板中查看时,看到以下错误:'未定义jQuery'.嗯,我认为这很容易-可能是jQuery文件未正确包含或未找到等.因此,我看一下HTML并单击该节点-瞧, jQuery脚本已被删除.正确包含在页面中.

I am using FF for debugging. When I look in the console panel, I see the following error: 'jQuery is not defined'. Ah, that's an easy one I think - maybe the jQuery file has not been included correctly, or not found etc. So I take a look at the HTML and click on the node - lo and behold, the jQuery script has been CORRECTLY included in the page.

尽管在引用jQuery的页面中没有其他js库使用"$"(如原型),但我已经在jQuery逻辑中调用了noConflict()方法,以防万一我稍后使用冲突的库阶段.

Although none of the other js libraries in the pages where jQuery is referenced uses '$' (like prototype), I have invoked the noConflict() method in the jQuery logic, just in case I use a conflicting library at a later stage.

我认为问题与我在模板环境(准确地说是Symfony 1.4)中使用jQuery的文件有关.对于那些不熟悉Symfony模板系统的人来说,视图模型基本上由一个布局"文件(模板)组成,然后进行修饰(装饰器模式)以及其他一些信息(以下称为页面内容") ).

I think the problem is related to the file that I am using jQuery in a templating environment (Symfony 1.4 to be precise). For those not familiar with the Symfony templating system, essentially, the view model is comprised of a 'layout' file (the template), which then gets decorated (decorator pattern), with other bits of information (lets call this 'page content').

最后一页大致如下所示:

The final page looks roughly something like this:

<html>
  <head>
  <script type="text/javascript" src= "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script>
  </head>
  <body>
   <!-- page contents go here -->
  </body>
</html>

我正在将jQuery加载到模板中(即Symfony术语中的布局").

I am loading jQuery into the template (i.e. the 'layout' in Symfony terminology).

这样做的原因是JQ库是在客户端缓存的,并且可用于需要它的页面.动态页面(其内容进入布局的页面内容"部分)将具有自己的jQuery逻辑.这(使用下面的马克·舒尔休斯(Mark Schultheiss)答案中提供的包装函数"想法)看起来像这样:

The reasoning for this being that the JQ library is cached client side, and is available for the pages that require it. The dynnamic pages (whose content go into the 'page content' section of the layout), will then have their own jQuery logic. This (using the "wrapper function" idea provided in the answer by Mark Schultheiss below), looks like this:

<script type="text/javascript">
/*<![CDATA[*/
jQuery(function()
{
 jQuery.noConflict();
 jQuery(document).ready(function(){
  jQuery("div.tpaccordion div.accItem").hide();
  jQuery("#latestVideosHolder").show();
  jQuery("div.tpaccordion h3").click(function(){
   jQuery(this).next().slideToggle("slow")
   .siblings("div.accItem:visible").slideUp("slow");
   jQuery(this).toggleClass("active");
   jQuery(this).siblings("h3").removeClass("active");
  });
 });
});
/*]]>*/
</script>

** [Edit2] **更正了语法错误.现在,我返回到jQuery未定义错误. :(

****Corrected the syntax error. Now I'm back to getting the jQuery is not defined error. :(

推荐答案

它在加载脚本/DOM之前正在运行. 把jQuery.noconflict();在一个jQuery包装器中

It is running before the script/DOM is loaded. Put the jQuery.noconflict(); in a jquery wrapper

jQuery(function()
{
  jQuery.noConflict(); 
});

如果您确实认为您可能需要保护自己:

If you DO think you might need to protect yourself:

  (function($)
    {
      $.noConflict(); 
    })(jQuery);

也可以

修复了一些语法错误

这篇关于jQuery未定义(在[Symfony]模板环境中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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