jquery插件方法的概念

发布于 2024-12-27 01:04:06 字数 1424 浏览 0 评论 0原文

我刚刚开始制作我自己的 jquery 插件。我仍然没有完全理解使用它们并在插件中添加方法的概念,我最近的尝试失败了,我不知道为什么。在下面的情况下,我得到了用于执行 capFirst 的常规代码,我想将其转换为插件。.

HTML

<div id="contain">
    <input type="text" /><br />
    <input type="text" /><br />
    <input type="text" />
</div>

原始 jquery

function capitaliseFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}

$('#contain > input').keyup(function(){
    var $this = $(this);    
    $this.val(capitaliseFirstLetter($this.val()));
});

我的尝试插件代码

    (function($) {

    $.fn.capFirst = function () {        
            $(this).keyup(function(){
                var $this = $(this);    
                $this.val(methods.capitaliseFirstLetter($this.val()));
            });      
    };

    var methods = function () {       
        capitaliseFirstLetter: function(inputValue) {
              return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
        } 
    };

})(jQuery);

$('#contain > input').capFirst();

jsfiddle: http://jsfiddle.net/ypWTd/118/

更新
jsfiddle 中的工作解决方案: http://jsfiddle.net/ypWTd/163/

I'm just starting of by making my own jquery plugins. I still havent wrapped my head around the concept of working with them and adding methods in the plugin and my recent attempt is failing and i dont know why. In this case bellow I got my regular piece of code i use to perform a capFirst and i want to transform that to the plugin..

HTML

<div id="contain">
    <input type="text" /><br />
    <input type="text" /><br />
    <input type="text" />
</div>

Original jquery

function capitaliseFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}

$('#contain > input').keyup(function(){
    var $this = $(this);    
    $this.val(capitaliseFirstLetter($this.val()));
});

My try of plugin code

    (function($) {

    $.fn.capFirst = function () {        
            $(this).keyup(function(){
                var $this = $(this);    
                $this.val(methods.capitaliseFirstLetter($this.val()));
            });      
    };

    var methods = function () {       
        capitaliseFirstLetter: function(inputValue) {
              return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
        } 
    };

})(jQuery);

$('#contain > input').capFirst();

jsfiddle: http://jsfiddle.net/ypWTd/118/

UPDATE
working solution in jsfiddle: http://jsfiddle.net/ypWTd/163/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说谎友 2025-01-03 01:04:06

该问题是由以下部分引起的:

var methods = function () {       
    capitaliseFirstLetter: function(inputValue) {
          return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
    } 
};

这实际上会导致语法错误。您似乎混淆了对象文字所需的语法和“类”所需的语法(需要使用 new 运算符创建其实例)。如果您只是将其设为对象文字,而不是函数,它将起作用:

var methods = {       
    capitaliseFirstLetter: function(inputValue) {
          return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
    } 
};

这是一个 更新的小提琴

另请注意,在您的插件方法中,this 已经是 jQuery 的实例,因此您不需要再次将其传递给 jQuery:

 $(this).keyup(function() {  //$(this)...
 this.keyup(function() { //Can just be this

Update

另一种方式(使用function 而不是对象文字)会像这样:

$.fn.capFirst = function () {
    this.keyup(function(){
        var $this = $(this),  
            m = new methods(); //Create an instance of methods
        $this.val(m.capitaliseFirstLetter($this.val())); //Call the method
    });                  
};
var methods = function() {       
    this.capitaliseFirstLetter = function(inputValue) {
          return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
    } 
};

这是一个 示例 该版本的。

The problem is caused by the following section:

var methods = function () {       
    capitaliseFirstLetter: function(inputValue) {
          return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
    } 
};

This actually causes a SyntaxError. You seem to mixing up the syntax between that required for an object literal, and that required for a "class" (an instance of which would need to be created with the new operator). If you just make it an object literal, rather than a function, it will work:

var methods = {       
    capitaliseFirstLetter: function(inputValue) {
          return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
    } 
};

Here's an updated fiddle.

Also note that in your plugin method, this is already an instance of jQuery so you don't need to pass it in to jQuery again:

 $(this).keyup(function() {  //$(this)...
 this.keyup(function() { //Can just be this

Update

The other way (using a function instead of an object literal) would go something like this:

$.fn.capFirst = function () {
    this.keyup(function(){
        var $this = $(this),  
            m = new methods(); //Create an instance of methods
        $this.val(m.capitaliseFirstLetter($this.val())); //Call the method
    });                  
};
var methods = function() {       
    this.capitaliseFirstLetter = function(inputValue) {
          return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
    } 
};

Here's an example of that version.

懷念過去 2025-01-03 01:04:06

唯一明显的区别是您用于“this”的双 $($())。您是否尝试过删除其中一个 $()?

其次:通常您应该“返回此”以启用链接。

第三:通常您应该通过在插件中使用 .each() 来提供对多个元素选择器的支持

this.each(function() {
   $(this).keyup(...);
})

第四:您尝试在不首先使用“new”运算符的情况下访问类的方法。要么你应该这样做

var myObject = {method: function() {...}}

myObject.method();

,要么你应该首先实例化对象

var myObject = function() {
    function method() {
    }
}

var myInstance = new myObject();
myInstance.method();

The only obvious difference is the double $($()) you are using for "this". Did you ever try to remove one of those $()?

Second: Usually you should "return this" to enable chaining.

Third: Usually you should provide support for multiple element selectors by using .each() inside the plugin

this.each(function() {
   $(this).keyup(...);
})

Fourth: You are trying to access a method of a class without using the "new" operator first. Either you should do

var myObject = {method: function() {...}}

myObject.method();

or you should instantiate the object first

var myObject = function() {
    function method() {
    }
}

var myInstance = new myObject();
myInstance.method();
天冷不及心凉 2025-01-03 01:04:06
(function($) {

    $.fn.capFirst = function () {
        return this.keyup(function() { // Returning the elements to maintain chainability
            var $this = $(this);    
            $this.val(methods.capitaliseFirstLetter($this.val()));
        });                  
    };

    var methods = { // Made this an object/array instead of a function that didn´t return anything.
        capitaliseFirstLetter: function(inputValue) {
            return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
        } 
    };

})(jQuery);

$(document).ready(function() { // Selectors should not be used before the DOM is ready.
    $('#contain > input').hide().capFirst().show().css('color', 'blue'); // Demo. chainability
});

jQuery 文档包含一些关于插件/Authoring 的有用信息。

您可以在此处找到我的完整编辑。

(function($) {

    $.fn.capFirst = function () {
        return this.keyup(function() { // Returning the elements to maintain chainability
            var $this = $(this);    
            $this.val(methods.capitaliseFirstLetter($this.val()));
        });                  
    };

    var methods = { // Made this an object/array instead of a function that didn´t return anything.
        capitaliseFirstLetter: function(inputValue) {
            return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);  
        } 
    };

})(jQuery);

$(document).ready(function() { // Selectors should not be used before the DOM is ready.
    $('#contain > input').hide().capFirst().show().css('color', 'blue'); // Demo. chainability
});

jQuery documentation contains some good information about Plugins/Authoring.

You´ll find my complete edit here.

記柔刀 2025-01-03 01:04:06

这就是您的插件应该如何

jQuery.fn.capFirst = function () {
    $(this).keyup(function(){ 
        inputValue = $(this).val();
        inputValue = inputValue.charAt(0).toUpperCase() + inputValue.slice(1);
        $(this).val(inputValue);
    });                        
};

jQuery(function() {
    $('#contain > input').capFirst();   
});

查看更新的 jsFiddle

This is how your plugin should be

jQuery.fn.capFirst = function () {
    $(this).keyup(function(){ 
        inputValue = $(this).val();
        inputValue = inputValue.charAt(0).toUpperCase() + inputValue.slice(1);
        $(this).val(inputValue);
    });                        
};

jQuery(function() {
    $('#contain > input').capFirst();   
});

Check out the updated jsFiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文