Jquery plugins: how to access options? -
Currently I'm writing a jQuery plugin with some options.
Simplified piece of code from a web page with an example:
And here (again simplified) plugin code:
(function ($) {$ .fn.myFunc = function (options) {/ / Default settings expansion option = $. Product ({width: 300, height: 200}, option); This is doing something for example with return.each (function () {// # div1 $ (this) .) {// Here I need to use the option of any other (like # divis) object / / How can I do this?}}}}}}}}) (JQuery);
Well, this question is in the list - How can I use the options of the second object from within the plug-in function? Something like $ ("# Div2") Options.width
You set up those options before you enter your plugin For .data (key, val) method:
// set 'option' for '# div2' $ ("# div2"). Data ('option', {width: 500, height: 500}); // Get 'Options' for '# div2' (this will be in your plugin code) var opts = $ ('# div2'). Data ('option'); Warning ('options.height:' + opts.height + '\ n' 'options.width:' + opts.width);
As you are collecting data in a dictionary-like object, you can set any type of data:
$ (" # Div2 "). Data ('priority', 2); $ ("# Div2") Data ('flag color', ['red', 'white', 'blue']); $ ("# Div2") Data ('part', {arm: 2, foot: 2});
... and find it later:
var foo = $ ("# div2"). Data ('priority'); Var foo2 = $ ("# div2") Data ('flagColors'); Var foo3 = $ ("# div2") Data ('part');
Behind the Scenario, jQuery adds a single extension-property to the DOM element of your jQuery selection (an internally generated UUID value), which costs a unique key in jQuery.cache That's basically where all your data is being collected / being retrieved from
For cleaning, call .removeData (key) (if no key is given, all data is deleted).
Comments
Post a Comment