简体   繁体   English

jQuery .fn说“不是函数”

[英]jQuery .fn is saying “not a function”

When I call this custom function 当我调用这个自定义函数时

$.fn.inputBoxHelper = function () {
    var args = arguments[0] || {};
    var matchingElem = $.grep(this, function (e) { return $(e).val() == $(e).attr('title'); });
    $(matchingElem).addClass(args.className);

    this.bind({
        focus: function(){
            if ($(this).val().trim() == $(this).attr('title')) { $(this).val(emptyString).removeClass('gray'); }
        },
        blur: function(){
            if ($(this).val().trim() == emptyString) { $(this).val($(this).attr('title')).addClass('gray'); }
        }
    });
    return this;
}

Like this 像这样

$('input:text').inputBoxHelper({ className: 'gray' });

It gives me the error when I call it 当我打电话给它时,它给了我错误

$("input:text,input:checkbox").inputBoxHelper is not a function
file:///D:/repository/scripts/script_tests/test.js
Line 20

Even when I change the function to just 即使我将功能更改为just

$.fn.inputBoxHelper = function () {return this;}

it does the same thing. 它做了同样的事情。 It seems to run fine and work correctly as all the functionality works on the input boxes but get the error. 它似乎运行正常并且正常工作,因为所有功能都在输入框上工作,但是会出错。 Anyone have any ideas? 有人有想法么?

Check that you're not loading jQuery into the page twice. 检查您是否未将jQuery加载到页面中两次。 Some plugins or plugin libraries like to include jQuery. 一些插件或插件库喜欢包含jQuery。 So it can overwrite your existing jQuery instance and override the $ and jQuery variables with a new – fresh – instance causing existing plugins to be undefined. 所以它可以覆盖你现有的jQuery实例,并用一个新的 - 新实例覆盖$jQuery变量,导致现有的插件未定义。 Plugins are added to the prototype of jQuery when they are defined. 在定义jQuery时,插件会添加到jQuery的原型中。 So if you destroy the only instance of that object, then the prototype is reset to its original form. 因此,如果您销毁该对象的唯一实例,则原型将重置为其原始形式。 And therefore those plugins are no longer part of that prototype. 因此,这些插件不再是原型的一部分。

Also, it may be nothing, JSLint is reporting that you're missing a semicolon on the last line of that function definition you posted. 此外,它可能没什么, JSLint报告你在你发布的那个函数定义的最后一行丢失了一个分号。 It ought to be: 它应该是:

$.fn.inputBoxHelper = function () {
  var args = arguments[0] || {};
  var matchingElem = $.grep(this, function (e) { return $(e).val() == $(e).attr('title'); });
  $(matchingElem).addClass(args.className);

  this.bind({
     focus: function(){
          if ($(this).val().trim() == $(this).attr('title')) { $(this).val(emptyString).removeClass('gray'); }
      },
      blur: function(){
          if ($(this).val().trim() == emptyString) { $(this).val($(this).attr('title')).addClass('gray'); }
      }
  });
  return this;
};

It's always a good idea to run all of your javascript through JSlint. 通过JSlint运行所有的javascript 总是一个好主意。 It can help you find stuff that you might spend hours looking for otherwise. 它可以帮助您找到您可能花费数小时寻找的东西。

The plugin needs to be loaded before it can be used. 插件需要先加载才能使用。

Comments above confirm that OP was attempting to use the plugin before it was loaded. 上面的评论证实OP在加载之前试图使用该插件。

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

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