【问题标题】:Uncaught ReferenceError: Masonry is not defined未捕获的 ReferenceError:未定义砌体
【发布时间】:2013-07-27 10:58:20
【问题描述】:

我正在处理我的作品集,我正在使用一个名为 Gridly 的 wordpress 主题,它使用 masonry。我正在努力使帖子向右而不是向左对齐。我遇到了一个选项here,它允许我通过使用 "isOriginLeft": false 来做到这一点,但现在我只是不断收到这个错误 "Uncaught ReferenceError: Masonry is not defined",而且我离让帖子更近了右对齐。

我的作品集在这里:brittonhack.com/new/

jQuery 不是我的强项,所以任何帮助都将不胜感激。谢谢!

这是不断产生错误的代码。

// masonry code 
$(document).ready(function() {
  $('#post-area').masonry({
    // options
    itemSelector : '.post',
    // options...
  isAnimated: true,
  animationOptions: {
    duration: 400,
    easing: 'linear',
    queue: false
  }

  });
});


// hover code for index  templates
$(document).ready(function() {

        $('#post-area .image').hover(
            function() {
                $(this).stop().fadeTo(300, 0.8);
            },
            function() {
                $(this).fadeTo(300, 1.0);
            }
        );  


});


// comment form values
$(document).ready(function(){
    $("#comment-form input").focus(function () {
        var origval = $(this).val();    
        $(this).val("");    
        //console.log(origval);
        $("#comment-form input").blur(function () {
            if($(this).val().length === 0 ) {
                $(this).val(origval);   
                origval = null;
            }else{
                origval = null;
            };  
        });
    });
});


// clear text area
$('textarea.comment-input').focus(function() {
   $(this).val('');
});

var container = document.querySelector('#post-area');
var msnry = new Masonry( container, {
  itemSelector: '.post'
});

【问题讨论】:

  • 您不需要多个jQuery文档准备功能,您可以将您拥有的三个逻辑合二为一;虽然执行多个就绪函数是合法的,但其他人查看您的代码时会感到困惑。
  • 你在哪里引用masonry 脚本?

标签: jquery wordpress


【解决方案1】:

您已经在文档顶部初始化砌体:

$(document).ready(function() {
    $('#post-area').masonry({
        // options
        itemSelector : '.post',
        // options...
    isAnimated: true,
    animationOptions: {
        duration: 400,
        easing: 'linear',
        queue: false
    }

    });
});

最后你再次初始化(不使用 jQuery):

var msnry = new Masonry( container, {
    itemSelector: '.post'
});

第二个块是多余的并抛出错误。删掉就好了。

【讨论】:

  • 谢谢,这消除了那里的错误,但我似乎仍然无法让帖子刷新对吗?有什么建议吗?
  • 尝试将选项isOriginLeft: false直接添加到js-masonry选项中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2016-11-03
  • 2011-01-05
  • 2016-01-02
  • 2013-10-06
  • 2016-12-17
相关资源
最近更新 更多