Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Safari-Money.js仅对第一行表应用格式_Javascript_Jquery_Html_Currency - Fatal编程技术网

Javascript Safari-Money.js仅对第一行表应用格式

Javascript Safari-Money.js仅对第一行表应用格式,javascript,jquery,html,currency,Javascript,Jquery,Html,Currency,我使用money.js和accounting.js进行格式化。该过程是,当页面加载时,它从URL(如果有)中获取#值,并将表行中的每个值转换为所选的转换 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="css/basictable1.css" /> <link href="https://fonts.googleapis.com/css?fa

我使用money.js和accounting.js进行格式化。该过程是,当页面加载时,它从URL(如果有)中获取
#
值,并将表行中的每个值转换为所选的转换

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/basictable1.css" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto:700" rel="stylesheet">


<script type="text/javascript" src="money.js"></script>
<script type="text/javascript" src="accounting.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>


<!-- Below are the exchange rates and the base currency -->
<script>
fx.base = "USD";
fx.rates = {
    "EUR" : 0.745101, // eg. 1 USD === 0.745101 EUR
    "GBP" : 0.647710, // etc...
    "HKD" : 7.781919,
    "CNY" : 8000,
    "USD" : 1,        // always include the base rate (1:1)
    /* etc */
}

var symbols = {
    "EUR" : '€',
    "GBP" : '£',
    "HKD" : 'HK$',
    "CNY" : '¥',
    "USD" : '$', 
}
var currentConversion = "USD";

</script>






<h2>money.js & accounting.js</h2>
<p>On first load of the page, values are returned from the server as shown in the table in USD (US DOLLARS). They should be formatted with javascript (accounting.js) to have the correct decimals 
and the correct currency symbol ($).<br><br>
The Correct formatting is as follows: <br><br>
<u>For Column Price:</u><br>
<br>Prices below 1 should be formatted with 6 decimals (e.g. 0.123456)<br>
Prices above 1 should be formatted with 2 decimals (e.g. 1.23)<br><br>
<u>For Column Month & Column Year:</u><br>
Prices should be formatted without decimals (e.g. 98473)<br><br>
</p>

<p>When the toggle is used and a different currency is selected, money.js should be used to calculate the price from the base (USD) to the new price using the exchange rate in 
the html page. Also the numbers should be formatted correctly, and the correct symbol for the currency should be added. The value of the dropdown should also be set to the new currency. </p>

<p><a href="http://openexchangerates.github.io/money.js/" target="blank">money.js docs</a></p>
<p><a href="http://openexchangerates.github.io/accounting.js/" target="blank">accounting.js docs</a></p>

<div class="dropdown-currency">
  <span class="selected-currency">USD</span> <span class="arrow-down"></span>
  <div class="dropdown-content">
    <ul class="dropdown-menu" role="menu">                            


        <li><a href="#USD" class="price-toggle" data-currency="usd">USD</a></li>
        <li><a href="#CNY" class="price-toggle" data-currency="cny">CNY</a></li>
        <li><a href="#EUR" class="price-toggle" data-currency="eur">EUR</a></li>
        <li><a href="#GBP" class="price-toggle" data-currency="gbp">GBP</a></li>
        <li><a href="#HKD" class="price-toggle" data-currency="hkd">HKD</a></li>

    </ul>
  </div>
</div>

<table id="table-two-axis" class="two-axis">
    <thead>
        <tr>
            <th>#</th>
            <th>FRUITS</th>
            <th>PRICE</th>

            <th class="invisible">MONTH</th>


            <th class="invisible">YEAR</th>

        </tr>
    </thead>
  <tbody>

        <tr>
                <td>1</td>
                <td>Apples</a></td>
                <td>222.22</td>
                <td class="invisible"> 2360096267</td>
                <td class="invisible"> 21213536108</td>
            </tr>

        <tr>
                <td>2</td>
                <td>Pears</td>
                <td>333.44</td>
                <td class="invisible"> 978984251</td>
                <td class="invisible"> 43618713955</td>
            </tr>

        <tr>
                <td>3</td>
                <td>Oranges</td>
                <td>44.43</td>
                <td class="invisible">  259375189</td>
                <td class="invisible">2460278427</td>
            </tr>

        <tr>
                <td>4</td>
                <td>Kiwi</td>
                <td>0.371500</td>
                <td class="invisible"> 160084682</td>
                <td class="invisible">6946057745</td>
            </tr>

        <tr>
                <td>5</td>
                <td>Tomatoes</td>
                <td>15.73</td>
                <td class="invisible">118967668</td>
                <td class="invisible">1474397171</td>
            </tr>
        </table>

<script type="text/javascript">
    // Separate this function so we can re-use it
    function formatCurrencies(desiredConversion, symbol){

        // Change selected text in the dropdown
        $('.selected-currency').html(desiredConversion);
        // Get actual array of children from table tbody : table rows
        var table = $('table tbody').children().toArray();
        // We loop through tables to actually convert each value
        table.forEach(function(tr, key){

            var tdAmount = $(tr).children('td:nth-child(3)');
            var tdMonth = $(tr).children('td:nth-child(4)');
            var tdYear = $(tr).children('td:nth-child(5)');

            // Unformat prices first to avoid NaN errors because of delimiters
            var price = accounting.unformat(tdAmount.text()); 
            var month = accounting.unformat(tdMonth.text()); 
            var year = accounting.unformat(tdYear.text()); 

            price = fx(price).from(currentConversion).to(desiredConversion);

            if(price > 1){
                // Two decimal places

                tdAmount.html(accounting.formatMoney(price, {
                    symbol: symbol,
                    format: "%s %v",
                    precision: 2
                }));
            } else {
                // Six decimal places
                tdAmount.html(accounting.formatMoney(price, {
                    symbol: symbol,
                    format: "%s %v",
                    precision: 6
                }));
            }

            month = fx(month).from(currentConversion).to(desiredConversion);
            year = fx(year).from(currentConversion).to(desiredConversion);

            tdMonth.html(accounting.formatMoney(month, {symbol: symbol, format: "%s %v", precision: 0}));
            tdYear.html(accounting.formatMoney(year, {symbol: symbol, format: "%s %v", precision: 0}));

            // Assign the new currentConversion
            localStorage.setItem('currentConversion', currentConversion);
            currentConversion = desiredConversion;
        })
    }

    $(document).ready(function(){

        var href = "";
        if (location.href.indexOf("#") != -1) {
            href = location.href.substr(location.href.indexOf("#") + 1)
        } else {
            href = "USD";
        }

        formatCurrencies(href, symbols[href]);


        $('.dropdown-menu li a').click(function(e){
            // We get the value of the "data-currency" attribute of the selected item
            var desiredConversion = $(this).data('currency').toUpperCase();
            var symbol = symbols[desiredConversion]; // Access the symbol by key index of symbols object
            formatCurrencies(desiredConversion, symbol);
        })
    })
</script>

</body>
</html>

fx.base=“美元”;
外汇汇率={
“欧元”:0.745101,例如1美元===0.745101欧元
“英镑”:0.647710,//等。。。
“HKD”:7.781919,
“人民币”:8000元,
“USD”:1,//始终包括基本汇率(1:1)
/*等*/
}
变量符号={
“欧元”:“欧元”,
“英镑”:“英镑”,
“HKD”:“HK$”,
“人民币”:“¥”,
“美元”:“$”,
}
var currentConversion=“美元”;
money.js和accounting.js
在第一次加载页面时,将从服务器返回值,如表中所示,单位为美元(US$)。它们应该用javascript(accounting.js)格式化,以获得正确的小数
以及正确的货币符号($)。

正确的格式如下:

对于列价格:

低于1的价格应设置为6位小数(例如0.123456)
高于1的价格应使用2位小数(例如1.23)进行格式化

对于列月份和列年度:
价格的格式应不带小数(例如98473)

使用切换并选择其他货币时,应使用money.js使用中的汇率计算从基准(美元)到新价格的价格 html页面。此外,数字的格式应正确,并应添加正确的货币符号。下拉列表的值也应设置为新货币

美元
# 果实 价格 月 年 1. 苹果 222.22 2360096267 21213536108 2. 梨 333.44 978984251 43618713955 3. 橘子 44.43 259375189 2460278427 4. 几维鸟 0.371500 160084682 6946057745 5. 西红柿 15.73 118967668 1474397171 //分离此函数,以便我们可以重复使用它 函数格式货币(所需转换、符号){ //更改下拉列表中的选定文本 $('.selected currency').html(需要转换); //从表tbody:表行中获取子级的实际数组 var table=$('table tbody').children().toArray(); //我们通过循环表来实际转换每个值 表.forEach(功能(tr,键){ var tdAmount=$(tr).children('td:n个children(3)'); var tdMonth=$(tr).children('td:nth children(4)'); 变量tdYear=$(tr).children('td:n个children(5)'); //首先取消对价格的格式化,以避免由于分隔符而出现NaN错误 var price=accounting.unformat(tdAmount.text()); var month=accounting.unformat(tdMonth.text()); var year=accounting.unformat(tdYear.text()); 价格=外汇(价格)。从(当前转换)。到(期望转换); 如果(价格>1){ //小数点后两位 tdAmount.html(accounting.formatMoney(price{ 符号:符号,, 格式:“%s%v”, 精度:2 })); }否则{ //小数点后六位 tdAmount.html(accounting.formatMoney(price{ 符号:符号,, 格式:“%s%v”, 精度:6 })); } 月=外汇(月)。从(当前转换)。到(期望转换); 年=外汇(年)。从(当前转换)。到(预期转换); html(accounting.formatMoney(月份,{symbol:symbol,格式:“%s%v”,精度:0})); html(accounting.formatMoney(年份,{symbol:symbol,格式:“%s%v”,精度:0})); //分配新的电流转换 setItem('currentConversion',currentConversion); currentConversion=所需转换; }) } $(文档).ready(函数(){ var href=“”; if(location.href.indexOf(“#”)!=-1){ href=location.href.substr(location.href.indexOf(“#”)+1) }否则{ href=“USD”; } 格式货币(href,符号[href]); $('.下拉菜单LIA')。单击(函数(e){ //我们获得所选项目的“数据货币”属性的值 var desiredConversion=$(this).data('currency').toUpperCase(); var symbol=symbols[desiredConversion];//通过symbols对象的键索引访问符号 格式货币(需要转换,符号); }) })

这适用于Chrome和Firefox。但是,它在Safari中不起作用。我有没有办法回避这个问题?谢谢。

你能提供
HTML
吗?你从哪里得到这些库,或者最好是一个演示?OscarJara我正在使用money.js和accounting.js。我已经用HTML更新了代码