-2
    jQuery.fn.extend({
    fillCurrency : (function() {
        $this = jQuery(this);

        function _usd(string1) {
            $this.text("$" + parseFloat(string1).toFixed(2));

            return $this;
        }

        return {
            usd : _usd
        };
    })()
});


var text = 2;
jQuery("#total").fillCurrency.usd(text);


<p id="total"></p>

尝试了几种选择,我想我现在已经接近了。现在的问题是 Object [object global] 没有方法“createDocumentFragment”。有人知道这里有什么问题吗?

4

2 回答 2

0

首先,我建议在函数的参数中传递货币,fillCurrency然后在您的代码中出现拼写错误

jQuery("#total").fillCurency.usd(text);

应该

jQuery("#total").fillCurrency.usd(text);

但即使你修正了你的错字,它也不会给你你想要的

这就是我的建议:

(function ($) {
    $.fn.fillCurrency = function (currency, value) {
        return this.each(function () {
            if (currency === "usd") {
                $(this).text(parseFloat(value).toFixed(2));
            }
        });
    }
})(jQuery);

$(function () {
    var text = 2;
    $("p#total").fillCurrency("usd", text);
});

http://jsfiddle.net/pWcBD/

于 2013-02-04T16:58:03.853 回答
0

我得到了解决方案:

jQuery.fn.extend({
Currency : function() {
        var $this = this;

        var _Fill = {
            usd : function(string) {
                $this.text("$" + parseFloat(string).toFixed(2));

                return $this;
   }
        }

        var _Get = {
            usd : function(string) {
                $this.text("$" + parseFloat(string).toFixed(2));

                return $this;
   }
        }

        return {
            Fill : _Fill,
            Get : _Get
        }
    }
});

这会将 jQuery 对象向下传递到内部对象中,例如插件。

于 2013-02-05T14:06:14.400 回答