未捕获的TypeError - 不是函数 - 没有视觉错误

时间:2015-07-09 09:55:54

标签: php jquery wordpress twitter-bootstrap

请在判断前阅读

是的,我完全清楚我的问题的次要标题。但是,我无法将其更改为具有更多解释标题的内容。因此,如果您有工作头衔,请告诉我。

好的,那我们就去找问题了。我正在使用同位素作为这个特殊部分,并使用Chrome Developer获得这个恼人的错误。我看不到任何可能与此问题相关的错误,但由于这是一个错误,我需要修复它(痴迷)。

Uncaught TypeError: b.isotope is not a function

我在网站上使用以下代码:

Isotope.js

jQuery(function ($) {

    var $container = $('#isotope-list'); //The ID for the list with all the blog posts
    $container.isotope({ //Isotope options, 'item' matches the class in the PHP
        itemSelector : '.item', 
        layoutMode : 'masonry'
    });

    //Add the class selected to the item that is clicked, and remove from the others
    var $optionSets = $('#filters'),
    $optionLinks = $optionSets.find('a');

    $optionLinks.click(function(){
    var $this = $(this);
    // don't proceed if already selected
    if ( $this.hasClass('selected') ) {
      return false;
    }
    var $optionSet = $this.parents('#filters');
    $optionSets.find('.selected').removeClass('selected');
    $this.addClass('selected');

    //When an item is clicked, sort the items.
     var selector = $(this).attr('data-filter');
    $container.isotope({ filter: selector });

    return false;
    });

});

PHP

<?php if ( $the_query->have_posts() ) : ?>
    <div id="isotope-list">
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 

        $termsArray = get_the_terms( $post->ID, "category");  //Get the terms for this particular item
        $termsString = ""; //initialize the string that will contain the terms
            foreach ( $termsArray as $term ) { // for each term 
                $termsString .= $term->slug.' '; //create a string that has all the slugs 
            }
        ?> 
        <div class="<?php echo $termsString; ?> item col-md-4"> 
            <ul class="grid cs-style-3">
                <li>
                    <figure>
                        <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
                        <?php if ( has_post_thumbnail() ) { 
                              the_post_thumbnail('frontpage_thumb');
                        } ?>
                        <figcaption class="lefttext">
                            <h3><?php the_title(); ?></h3>
                            <span class="offgrey"><?php echo(types_render_field( "produkt", array( 'raw' => true) )); ?> / <?php echo(types_render_field( "produsert", array( 'raw' => true) )); ?></span>
                            <a href="<?php the_permalink() ?>" rel="bookmark" class="smoothtrans">Se prosjekt</a>
                        </figcaption>
                    </figure>
                </li>
            </ul>             
        </div> <!-- end item -->
        <?php endwhile;  ?>
    </div> <!-- end isotope-list -->
    <script src="<?php bloginfo('stylesheet_directory'); ?>/js/toucheffects.js"></script>
<?php endif; ?>

可以找到该网站here

我已经尝试过了,但正如我所看到的那样,没有理由它变成b同位素作为#isotope-list。我已经在网上搜索了几次工作解决方案,但我找不到任何。一篇帖子说它与Bootstrap有关。更新到Bootstrap后我仍然看到问题,所以我相信Bootstrap没有错误。但是,Boostrap和Isotope之间可能存在以下关联问题:

$container

由于容器在我的网站上多次使用,可能会因Bootstrap中的某些内容而崩溃。

总结一下:

  • Chrome Developer
  • 中显示错误
  • 没有视觉错误
  • 使用Wordpress,Isotope和Bootstrap
  • 可以找到页面here

您知道任何有效的解决方案或想法吗?请告诉我。

我真的建议查看链接以查找解决方案,因为它可能会感染我未在此处发布的其他j。

2 个答案:

答案 0 :(得分:2)

问题是isotype.js被包含两次。

为避免这种情况,应始终按照wordpress提供/打算包含脚本的方式包含脚本。这可以确保脚本永远不会被包含多次,同时提供了一种在脚本之间添加依赖关系的便捷方式,确保脚本包含在正确的顺序中。

  • 从不自己编写<script src="[whatever]"></script>标记(永远不会唯一的方式)。

  • 始终包含使用wordpress函数wp_enqueue_script()codex的脚本,如果它们本身没有价值(例如jQuery),只需使用wp_register_script()codex)注册它们,以便它们可供其他脚本依赖。

  • 最好仅在wp_enqueue_scripts挂钩(codex)中包含脚本。

正确包含具有依赖项的脚本的示例:

function theme_name_scripts() {
    /**
     * isotope.js requires jQuery - wordpress will automatically make
     * sure jQuery is included _before_ isotope.js
     * Note that jquery does not need to be registered or enqueued
     * manually, as it is one of many standard scripts included with
     * wordpress... But the dependency should be specified explicitly
     * using the third parameter of wp_register_script() or
     * wp_enqueue_script()
     */
    wp_register_script( 'isotope', get_template_directory_uri() . '/js/isotope.js', array('jquery'), '1.0.0' );

    /**
     * my_theme.js requires both jQuery and isotype.js (referred to by
     * the script ID - first parameter of wp_register_script call above)
     * wordpress will automatically make sure isotype.js is included
     * _before_ my_theme.js - and when including isotype.js it will
     * notice that that depends on jQuery and make sure to also include
     * that.
     * Note that wordpress handles this even if this call is made before
     * registering or enqueuing isotope.js
     */
    wp_enqueue_script( 'my_theme', get_template_directory_uri() . '/js/my_theme.js', array('isotope'), '0.1.0' );
}

//Hook in to the appropriate hook to run the function:
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

始终以正确的方式做事将显着增加与第三方插件和未来版本的wordpress的兼容性,同时降低&#34;奇怪的&#34; /&#34;随机&#34;错误(即人为错误)

答案 1 :(得分:0)

据我所知,b.isotope实际上是函数的正确名称,因为它已被缩小(参见isotope.js)。在尝试将同位素插件绑定到HTML元素之前,请确保包含同位素插件

编辑: 您是否尝试将代码包装在isotope.js

$(document).ready(function() {
     [...] your code [...]
});