English 中文(简体)
B. 杂草方法的概念
原标题:Concept of jquery plugin methods

我只是从自己做的 j花开始。 我仍然围绕与他们合作的概念总结我的头脑,在gin子里添加方法,我最近的努力失败了,我很想知道原因。 在该案中,我经常使用一只手法,以做头脑,并想把这改成花板。

<><><><>>><>>>>><>>>>>>

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

www.un.org/spanish/ecosoc

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

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

www.un.org/Depts/DGACM/index_spanish.htm 我的尝试

    (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:

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

最佳回答

问题由以下章节引起:

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

这实际上造成SyntaxError。 您似乎把 object子混在一起,把物体字字面要求与“舱”要求混为一谈(需要与<代码>新<>>/代码>操作者一起创建)。 如果你只是把它变成一个目标,而不是一个功能,那么它就会发挥作用:

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

Here s an updated fiddle.

又注意到在你的原始方法中,this 已经成为 j的典范,因此你无需再通过:子:

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

<>Update>

另一种方式(使用<条码>功能<>/条码>而不是物体字面)就是如此:

$.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);  
    } 
};

http://jsfiddle.net/ypWTd/138/“rel=“nofollow”>example

问题回答

唯一明显的差别是,你为“这”使用两美元((美元)。 您是否曾试图删除其中的一笔款项?

第二: 通常,你应“恢复”,以便能够进行链条。

第三:通常,你应使用原始材料中的 (ach)支持多种要素选择。

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

第四: 你们试图在使用“新”运营商之前使用一个类别的方法。 你们都应这样做。

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

myObject.method();

或您应首先对物体进行即时处理

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

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




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签