简体   繁体   English

如何通过重新加载javascript在两种货币之间切换?

[英]How to switch between two currencies by reloading javascript?

I have a javascript for counting price (it depends on many factors). 我有一个用于计算价格的JavaScript(取决于许多因素)。 I would like to have two currencies: euros and dollars. 我想要两种货币:欧元和美元。 Currently the default one is euro. 当前默认值是欧元。 I would like to have two buttons for switching between them - one for euros and one for dollars. 我想要两个按钮在它们之间进行切换-一个用于欧元,另一个用于美元。 I imagine it would work like this: I would make two javascripts (I already have one of them - in which I calculate the price - let's call him europrice.js). 我想它会像这样工作:我将制作两个javascript(我已经有一个javascript-在其中计算价格-我们称他为europrice.js)。 Now I would create another one called dollarprice.js in which I would calculate the price in dollars. 现在,我将创建另一个名为dollarprice.js的文件,在其中以美元计算价格。 In default the website would have loaded with europrice.js. 默认情况下,该网站将加载europrice.js。 By clicking on "dollarbutton" the price would have reloaded this time with dollarprice.js and not europrice.js. 通过单击“ dollarbutton”,这次的价格将是使用dollarprice.js而不是europrice.js重新加载的。 I have tried to make this clear as much as I can, hopefully you understand :) Thanks for any advice 我已尽力澄清了这一点,希望您能理解:)感谢您的任何建议

one point is, you may not need own js file, maybe a own function would be enough. 一点是,您可能不需要自己的js文件,也许拥有自己的功能就足够了。

Apart from that you could wrap your implementation by an api-like call, and switching the implementation on click: 除此之外,您还可以通过类似api的调用包装实现,然后在点击时切换实现:

var PriceCalculator = {
    calculateInEuros: function() {
        //your euro logic here
    },
    calculateInDollars: function() {
        //your dollar logic here
    },
    // "api-function" - default: euro
    calculatePrice: PriceCalculator.calculateInEuros
}

usage: 用法:

var price = PriceCalculator.calculatePrice();

switching: 交换:

PriceCalculator.calculatePrice = PriceCalculator.calculateInEuros;

or 要么

PriceCalculator.calculatePrice = PriceCalculator.calculateInDollars;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM