【问题标题】:#accordion is not a function#accordion 不是函数
【发布时间】:2012-01-26 19:53:41
【问题描述】:

手风琴脚本有问题,当我使用直接 html 使用手风琴时,手风琴可以工作,但现在转移到 wordpress 手风琴已经停止工作。

这是我的代码

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
  <script>
  $(document).ready(function() {
    $("#accordion").accordion();
  });
  </script>
<?php wp_head(); ?>

和html:

            <div id="accordion">
 <div class="button"><a href="#">Infrastructure</a></div>
    <div><?php
            if (function_exists('iinclude_page')){
                iinclude_page('sectors/infrastructure');
            }?>
            </div>
    <div class="button"><a href="#">Housing</a></div>
    <div><?php
            if (function_exists('iinclude_page')){
                iinclude_page('sectors/housing');
            }?></div>
                <div class="button"><a href="#">Education</a></div>
    <div><?php
            if (function_exists('iinclude_page')){
                iinclude_page('sectors/education');
            }?></div>
                <div class="button"><a href="#">Health</a></div>
    <div><?php
            if (function_exists('iinclude_page')){
                iinclude_page('sectors/health');
            }?></div>
</div>

我真的不明白这个问题,因为它在直接 html 中工作。我什至尝试使用纯文本删除包含功能,但它仍然不起作用?

有什么想法吗?也没有其他 jquery 对象干扰。

【问题讨论】:

  • 源代码中的 html 与您在浏览器中查看源代码时看到的 html 有什么区别吗?进行优化的插件将尝试挤出不必要的换行符。如果一般总是在空标签(脚本或其他)中放置一个 html 注释。

标签: jquery wordpress function


【解决方案1】:

您加载了 jQuery 两次(1.5 之后是 1.7.1),并在加载 1.5 之后但在加载 1.7.1 之前加载了 jQuery UI。所以这就是发生的事情:

  1. jQuery 1.5 已加载。

  2. jQuery UI 被加载并将其自身附加到此时存在的jQuery 对象。

  3. jQuery 1.7.1 已加载,完全替换了jQuery 对象。

  4. 您的代码运行,使用 (1.7.1) jQuery 对象,jQuery UI 没有添加它的优点。

如果你只加载一次 jQuery,并确保在之后加载 jQuery UI,它会工作的。

【讨论】:

  • 这正是我想说的 ;) 感谢您对为什么的解释。
  • 谢谢,这太奇怪了,因为我从 jquery 网站本身复制并粘贴了手风琴页面上的代码,其中包含代码中的 2 个版本!
  • @Amy:很高兴有帮助!我没有看到在 jqueryui.com 的 the accordion page 上加载了两个版本的 jQuery。
【解决方案2】:

你包含了 jQuery 两次,也许这就是问题所在?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
..
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

【讨论】:

    【解决方案3】:

    当你在 Wordpress 上运行它时,你能告诉我完整的 html。 我在想也许在包含页面中有一些未闭合的标签可能会损坏手风琴。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    

    并且尽量不要插入 jQuery 两次。

    【讨论】:

    • 他说得有道理,因为没有 php 标签可以正常运行:jsfiddle.net/zd6Hg
    • @gideon 我也做了同样的检查 :-) jsfiddle.net/JF8WY 但我想看看包含 Wordpress 的 html
    • 啊。是的,我想知道为什么有人-1,因为这是非常有效的,所以我+1 :)
    猜你喜欢
    • 2013-01-31
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多