English 中文(简体)
每一元素(金刚金)的自身变量
原标题:own variables for each element (jQuery plugin)

I m trying to understand, how jQuery plugins work. This is my first test plugin (it does nothing :) )

(function($)
{
    var methods =
    {
        init : function(options)
        {
            if (options)  $.extend(settings, options);
            myElement = $(this);
            return myElement;
        },
        getInfo : function()
        {
            console.log( Info =   + myElement.attr( id ) +   [  + settings.itemName +  ] );
        }
    };

    var settings = { itemName  :  item  };
    var myElement = null;


    $.fn.myTest = function(method)
    {
        if (methods[method])
        {
            return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method ===  object  || ! method)
        {
            return methods.init.apply(this, arguments);
        }
        else
        {
            $.error( Method   + method +   does not exist on jQuery.myTag );
        }
    };
})(jQuery);

我在超文本中称:

$(document).ready(function()
{
    t = $( #list ).myTest({itemName: First });
    t.myTest( getInfo );
    t2 = $( #l2 ).myTest({itemName: Hello World });

    t2.myTest( getInfo );
    t.myTest( getInfo );
});

我认为,假想的变数改变了我的特性:

Info = list [First] - OK
Info = l2 [Hello World] - OK
Info = l2 [Hello World] - Why???

What I need to write, that plugin variable myElement was different for each element? Thanks.

问题回答

我已经解决。 感谢。

    data = $this.data( test );

    if (!data)
    {
        $(this).data( test ,
        {
            element : $this,
            settings : settings
        });
    }




相关问题
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: &...

热门标签