简体   繁体   English

如何获得ready函数和另一个可以在document.ready中正常播放的函数?

[英]How do I get a ready function and another function to play nicely in document.ready?

In my post.js , I have this: 在我的post.js ,我有:

var ready;
ready = function() {

    // This is the Sidebar toggle functionality
    var toggleSidebar = $(".togglesidebar");
    var primary = $("#primary");
    var secondary = $("#secondary");

    toggleSidebar.on("click", function(){

        if(primary.hasClass("col-sm-9")){
            primary.removeClass("col-sm-9");
            primary.addClass("col-sm-12");
            secondary.css('display', 'none');
        }
        else {
            primary.removeClass("col-sm-12");
            primary.addClass("col-sm-9");
            secondary.css('display', 'inline-block');
        }
    }); 
};

$(document).ready(ready);

Which works MOST of the time. 大部分时间都可行。 There are times when the toggleSidebar doesn't toggle (eg when you go to a different page and come back to the main where the main button is...it doesn't toggle. I have to refresh the page before it starts working again...I suspect this is related to Turbolinks though)...but that's a side issue. 有时候, toggleSidebar不会切换(例如,当您转到其他页面并返回到主按钮所在的主窗口时……它不会切换。我必须刷新页面,然后它才能再次开始工作...我怀疑这与Turbolinks有关)...但这是附带问题。 That's not the core issue here. 这不是这里的核心问题。

I also have this: 我也有这个:

counter = function() {
    var body_value = $('#post_body').val();
    var title_value = $('#post_title').val();       

    if (body_value.length == 0) {
        $('#wordCountBody').html(0);
        return;
    }

    if (title_value.length == 0) {
        $('#wordCountTitle').html(0);
        return;
    }

    var regex = /\s+/gi;
    var wordCountBody = body_value.trim().replace(regex, ' ').split(' ').length;
    var wordCountTitle = title_value.trim().replace(regex, ' ').split(' ').length;

    $('#wordCountBody').html(wordCountBody);
    $('#wordCountTitle').html(wordCountTitle);
};

$(document).on('ready page:load', function () {
  $('#count').click(counter);
    $('#post_body, #post_title').on('change keydown keypress keyup blur focus', counter);
});

Which doesn't work at all. 这根本不起作用。 In fact, whenever I put my cursor in the #post_title textfield in my JS console, I get this error: 实际上,每当我将光标放在JS控制台的#post_title文本字段中时,都会出现此错误:

Uncaught TypeError: Cannot read property 'length' of undefined

Which I suspect is related to the above. 我怀疑与以上有关。

What could be causing this? 是什么原因造成的?

Also, I am not sure if the order of these statements in my post.js file actually matters, I changed the order for the purposes of this question - so it is more clear. 另外,我不确定post.js文件中这些语句的顺序是否确实重要,因此出于这个问题的目的,我更改了顺序-这样更清楚了。

However, in my actual post.js , the order is: 但是,在我实际的post.js ,顺序为:

var ready;
ready function() declaration
counter function() declaration
$(document).ready(ready);
$(document).on('ready page:load', function () {
  $('#count').click(counter);
    $('#post_body, #post_title').on('change keydown keypress keyup blur focus', counter);
});

All of this feels wrong, I just don't know how to combine them and clean this up. 所有这些都感觉不对,我只是不知道如何将它们结合起来并进行清理。

Thoughts? 思考?

Then, it is clear that body_value is undefined and that happens because $('#post_body').val() returns undefined and that can happen if #post_body doesn't exist or if it isn't a form object that has a .val() method. 然后,很明显,body_value是未定义的,这是因为$('#post_body').val()返回undefined,并且如果#post_body不存在或者它不是具有的表单对象,则可能会发生.val()方法。 I think we'd have to see your HTML to advise further, but it appears that #post_body does not exist. 我认为我们必须看一下HTML才能提供进一步的建议,但看来#post_body不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM