0

我有一个网站,Hostingshine.com

我想要一个脚本工作,但一个正常的,否则你可以看到这个

$(document).ready( function() { 
    $('#usd').click(function(){
    $('.usd').show();
    $('.inr').hide();
    })

    $('#inr').click(function(){
    $('.inr').show();
    $('.usd').hide();
    })
          });  

将它与 Jquery 一起使用,

页面是这样的

<ul id="currencychange"><li>
  <a id="usd">USD</a>&nbsp;<a id="inr">INR</a>
</li></ul>

并将类 inr 和 usd 放到我想显示所需货币的地方,但每次我更改页面或刷新页面时,它都会返回默认值,即 usd。

请帮我解决这个问题,帮我获取 cookie 功能或 geo 功能以使用此代码...

提前致谢。

4

2 回答 2

1
You can use geoplugin library from here 
http://www.geoplugin.com/webservices/javascript
and do something like following code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            var currencyCode = geoplugin_currencyCode();
            $('#currencychange').children().children().each(function () {
                if ($(this).attr('id') != currencyCode.toLowerCase()) {
                    $(this).hide();
                }


            });


        });
    </script>
</head>
<body>
<ul id="currencychange">
    <li>
        <a id="usd">USD</a>&nbsp;<a id="gbp">GBP</a>
    </li>
</ul>

</body>
</html>
于 2012-06-26T14:19:45.047 回答
1

这是完整的解决方案 David:您必须添加对 geoplugin 和 money.js 的引用,就像我在下面的代码中所做的那样。我还在每一行之后添加了注释。如果你不明白任何事情只是问。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>


    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

    <!--Add a reference to geoplugin -- we will use this to get geo/local settings -->
    <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>

    <!--Add a reference to money.js -- we will use this to convert our currency -->
    <script type="text/javascript" src="http://josscrowcroft.github.com/money.js/money.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {

            // get local currency code using geo plugin
            var localCurrencyCode = geoplugin_currencyCode();


            // make an ajax call to open exchange rates
            $.ajax({
                url: 'http://openexchangerates.org/latest.json',
                dataType: 'jsonp',
                success: function(json) {
                    // Rates are in `json.rates`
                    // Base currency (USD) is `json.base`

                    // set the returned values to money.js object
                    // we will use this later for currency conversion
                    fx.rates = json.rates;
                    fx.base = json.base;

                    // get exchange rate for local currency
                    var exchangeRate = json.rates[localCurrencyCode];

                    // get all elements on page that has a class of currency (you can use the same logic for selection or have your own)
                    $(".currency").each(function () {
                        // extract price eg if $10.00 get 10
                        var priceRaw = parseFloat($(this).html().replace('$',''));

                        // convert the price to local price
                        // we use money.js for this
                        var priceLocal = fx(priceRaw).from(fx.base).to(localCurrencyCode);


                        // finally we construct our price and inject it in the html
                        var finalPrice = localCurrencyCode +  priceLocal;
                        $(this).html(finalPrice) ;

                    });
                }
            });




        });
    </script>
</head>
<body>
<ul id="currencychange">
    <li>
        $10 = <a class="currency">$10</a><br>
        $20 = <a class="currency">$20</a><br>
        $30 = <a class="currency">$30</a><br>
    </li>
</ul>

</body>
</html>
于 2012-06-26T16:19:24.260 回答