【问题标题】:Uncaught TypeError: Cannot read property 'innerText' of undefined未捕获的类型错误:无法读取未定义的属性“innerText”
【发布时间】:2015-04-21 10:22:05
【问题描述】:

实际上我有两个错误,我不知道第一个来自哪里。我从“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
  • @void 它来自某个地方的 jQuery 包含。文件名为det.js
  • scrollbar 是什么?
  • 这是我要调用的函数
  • 插件是空的,我试图创建一个用于测试但我什至没有达到这个功能。

标签: javascript jquery google-chrome innertext


【解决方案1】:

不确定 innerText 与错误有什么关系,您应该只是收到错误 "Uncaught TypeError: undefined is not a function"

你制作插件的方式不对。这是一个基本的插件结构。注意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>

【讨论】:

  • 好吧,我知道了,但是fn$.fn.scrollbar 中代表什么?
猜你喜欢
  • 2021-12-22
  • 2015-01-06
  • 2017-07-26
  • 2019-02-26
  • 2019-10-19
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多