请先阅读后判断
是的,我完全意识到我的问题的标题较低。然而,我不能改变它的东西,有一个更解释的标题。所以,如果你有一个暂定的标题,请告诉我。
好的,我们开始提问。我使用同位素这个特殊的部分,并得到这个烦人的错误与Chrome开发人员。我看不到任何可能与此问题相关的错误,但由于这是一个错误,我需要修复它(痴迷)。
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; ?>
网址在这里
我试过查找它,但正如我所看到的,没有理由把它变成b.同位素作为#同位素列表。我在网上搜索了几次有效的解决方案,但我找不到任何解决方案。一个帖子说这和Bootstrap有关。更新到Bootstrap后,我仍然看到这个问题,所以我相信Bootstrap没有错误。然而,Boostrap和同位素之间可能存在以下连接问题:
$container
也许这是崩溃的东西从Bootstrap作为容器在我的网站上使用了几次。
总结一下:
- Chrome Developer 显示错误
- 无视觉错误
- 使用Wordpress,同位素和Bootstrap
- 页面可在这里找到
你知道什么可行的解决方案或想法吗?请让我知道。
我真的建议检查链接寻找解决方案,因为它可能会感染其他js我没有在这里发布。
问题原来是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未来版本的兼容性,同时减少"奇怪"/"随机"错误的风险。人为错误)
据我所知,b.isotope
实际上是函数的正确名称,因为它已经被缩小了(参见同位素.js)。在尝试将同位素插件绑定到HTML-elements
编辑:您是否尝试过在
中包装isotope.js
中的代码?$(document).ready(function() {
[...] your code [...]
});
?