创建基本Jquery插件?错误:预期对象,为什么?

时间:2012-06-07 06:47:56

标签: jquery jquery-plugins

我正在尝试构建自己的jquery插件。用简单的例子,但我在执行它时遇到错误。计划为

构建插件
  

贷款计算器

可以帮助我解决错误“

  

预期对象

我在index.html中的javascript就是这样...

$(function(){
    $('#btn').click(function() {
     var roi=getie();  //  ROI - rate of interest - value of i receved from function getie()
    $("#i").val(roi);  // input type 'text ' to display Interest (i) value
    });

});

和自定义函数的插件codgin

  

getie()

在单独的

中是这样的
  

calculate.js文件

代码:

(function($){
    //Attach this new method to jQuery
    $.fn.extend({ 
            //This is where you write your plugin's name
       getie: function() {
            //Iterate over the current set of matched elements
            return this.each(function() {
             var  i=20;
             return i;
             });
        }
    });


})(jQuery);

我正在使用jquery-1.7.2.min.js和query-ui-1.8.20.custom.min.js作为其他js文件。

3 个答案:

答案 0 :(得分:0)

你应该打电话

roi = $('someSelector').getie()

'Object expected'意味着你应该将一个对象传递给你的函数,在本例中是jQuery对象或一些jQuery包装的集合。

答案 1 :(得分:0)

使用$.fn.extend扩展jquery并且不创建对象。所以你必须在jquery对象上调用它:

$(this).getie()

答案 2 :(得分:0)

为什么你需要getie成为一个jQuery插件?你使用它的方式和 或者常规功能就足够了

function getie(){
}
相关问题