$(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进

$(document).ready and jQuery(function($) not working; jQuery is defined, but script isn't entering .ready function

本文关键字:jQuery 脚本 未定义 工作 ready document function 定义      更新时间:2023-09-26

编辑:

这是完整的HTML。太长了,我不能直接粘贴在这里。我没有创建这个页面,我只需要清理它的问题。


我正试图从$(document).ready内部运行一个函数,因为我需要在函数运行之前完成DOM的加载,因为我正在绑定按钮。这就是我在脚本中的内容:

jQuery(function($) {
    alert('foo');
    SI.regionSelector.init();
});

我也试过:

$(document).ready(function(){
    alert('foo');
    SI.regionSelecter.init();
});

但警报从未发生过。我已经完成了Chrome中的调试器。我点击jQuery(函数($),我没有得到任何错误(事实上控制台显示了jQuery的定义,所以我知道我的页面上有jQuery),然后调试器跳到jQuery函数的末尾,但它根本不会进入警报。

以下是完整的脚本:

var SI = SI || {};
SI.regionSelector = {
  settings: {},
  regions: [],
  init: function() {
    jQuery('a').click(function(e){ e.preventDefault(); });
    // Start the DOMSelection tool when a region is clicked
    jQuery('#region-selector .region').click(function(){
      targetRegion = jQuery(this);
      SetupDOMSelection();
    });
    // When saving, pull the values stored by the DOMSelection tool
    // and stuff them into hidden fields so they can be picked up on
    // the back end.
    jQuery('#region-selector .save').click(function(){
      jQuery('#site_ingestor_region_selector .btn.region').each(function(){
        var values = {};
        values.innerHTML = jQuery(this).data('innerHTML');
        values.xpath = jQuery(this).data('xpath');
        var id = jQuery(this).attr('id');
        var $hiddenInput = jQuery('input[name="'+id.replace('btn', 'form-region')+'"]');
        $hiddenInput.val(JSON.stringify(values));
      });
    });
    jQuery('.btn.edit').click(function() {
      jQuery('#site_ingestor_html_editor').css('display', 'block');
      jQuery('body').css('overflow', 'hidden');
      window.scrollTo(0, 0);
    });
    jQuery('.btn.cancel').click(function() {
      jQuery('#site_ingestor_html_editor').css('display', 'none');
      jQuery('body').css('overflow', 'scroll');
        });
      }
};
jQuery(function($) {
    alert('foo');
    SI.regionSelector.init();
});

我要检查的一些东西:

  • 确保在脚本之前引用jquery.js库
  • 确保您的$(或jQuery,取决于您使用的内容)不会与任何其他库冲突
  • 在浏览器的JS控制台中检查其他JS错误(或其他消息)

我还会运行一个完整的标记页验证(可能会修剪一些标记以降低复杂性),看看是否存在一些明显的HTML错误,比如缺少结束正文标记或类似的东西。