0

我正在尝试通过 jquery 向我的导航添加一个活动类:

<script type="text/javascript">
            $( '.nav li a' ).each(function() {
                $(this).removeClass('active');
            });
            $('.nav li a').eq(0).addClass("active");
        </script>

我正在使用 wordpress,可以看到 jquery 已定义:

<script type='text/javascript' src='https://artendijen.com/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script>

但是我在控制台日志中收到此错误:

Uncaught TypeError: Property '$' of object [object Object] is not a function

并且活动课程未添加到我的导航中。

我尝试将此代码放在 header.php 和 page.php 中,但给了我上面相同的错误。

这是我的CSS:

<div class="navigation">

    <div class="logo">
        <a href="/"><img src="/wp-content/themes/twentytwelve/images/logo.png" width="275" /></a>
    </div><!--logo-->

    <ul class="nav">
        <li><a href="/about-us">About Us</a></li>
        <li><a href="/shop">Gallery & Shopping</a></li>
        <li><a href="/latest-news">Latest News</a></li>
        <li><a href="/contact-us">Contact Us</a></li>
        <li><a href="/links">Links</a></li>
    </ul>

</div>

我能做些什么来解决这个问题?任何帮助,将不胜感激。

4

1 回答 1

1

您提供链接的 jQuery.js 文件的底部jQuery.noConflict()确实删除了$别名。

您应该能够通过将代码包装在以下内容中来解决它:

/* allows use of `$` in your code*/
(function($){
 /* wait for page to load before running code*/
  $(function(){
      /* your code here */
  });
})(jQuery);

确保在加载 jQuery.js 之后放置此代码的脚本标记

于 2013-11-03T05:04:37.953 回答