未捕获的类型错误:无法读取属性'innerText'的未定义

Uncaught TypeError: Cannot read property 'innerText' of undefined

本文关键字:属性 未定义 innerText 读取 类型 错误      更新时间:2024-01-23

实际上我有两个错误,我不知道第一个是从哪里来的。我包含了来自"code.jQuery.com"的jQuery v.2.1.3,Chrome向我显示了这个错误

未捕获类型错误:无法读取未定义的的属性"innerText"

我想第二个错误是我自己造成的。

<head>
    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
</head>
<body>
    <div class="mydiv" style="height:500px; width:500px; margin:50px auto; background-color:#ddd;">
    </div>
    <script type="text/javascript">
        (function($) {
            $.scrollbar = function(options){
                var opts = $.extend($.scrollbar.defaults, options);
            };
            $.scrollbar.defaults = {
                background: "yellow"
            };
        })(jQuery);
        $(function(){
            $('.mydiv').scrollbar();
            //Error: Uncaught TypeError: undefined is not a function
        });
    </script>
</body>

有人能帮我吗?我的谷歌技能已经到了极限,很快就要放弃了。

不确定innerText与错误有什么关系,您应该只得到错误"Uncaught TypeError:undefined不是函数"

您制作插件的方式是错误的。这是一个基本的插件结构。注意fn 的使用

(function($) {
  $.fn.scrollbar = function(options) {
    var opts = $.extend($.fn.scrollbar.defaults, options);
    
    return this.each(function() {
      $(this).css("background-color", opts.background);
    });
    
  };
  $.fn.scrollbar.defaults = {
    background: "yellow"
  };
})(jQuery);
$(function() {
  $('.mydiv').scrollbar();
  $('.myOtherDiv').scrollbar({ "background":"red"});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mydiv">XXXXXXXXXX</div>
<div class="myOtherDiv">XXXXXXXXXX</div>