Ich arbeite an einem jquery Plug-in und ich bin in einem Problem beim Speichern von Eigenschaften für die spätere Verwendung. Im Beispiel unten ist die Konsolenausgabe 18, 50, 50
, wenn ich nach 18, 50, 18
suche. Ich verstehe, warum dies passiert, aber ich kann keinen guten Weg finden, properties
für die Verwendung in mehreren verschiedenen Methoden zu speichern. Ich habe das Gefühl, dass mir etwas sehr offensichtlich fehlt, aber ich sehe es einfach nicht.jQuery Plugin Gespeicherte Werte
<html>
<body>
<h1>Hello</h1>
<h2>World</h2>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript">
(function ($) {
var commonOperations, methods, properties;
commonOperations = function() {
console.log(properties.height);
};
methods = {
init : function (overrides) {
var defaults;
defaults = { height: 18 };
properties = $.extend(defaults, overrides);
commonOperations();
},
foo : function() {
commonOperations();
}
};
$.fn.myPlugin = 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 for jQuery.myPlugin');
}
};
}(jQuery));
$(document).ready(function() {
$("h1").myPlugin();
$("h2").myPlugin({ height: 50 });
$("h1").myPlugin("foo");
});
</script>
</body>
</html>
+1 - Informationen zur Unterstützung finden Sie hier: http://docs.jquery.com/Plugins/ Authoring # Daten –
Danke. Das macht vollkommen Sinn. – Jason