Javascript 字符串中的美元符号

发布于 2025-01-02 11:54:13 字数 598 浏览 0 评论 0原文

我想解析一个元素并按货币设置区域。

HTML:

<span id="price">¥82,84</span><br/>

Javascript:

        price = document.getElementById("price").innerHTML;
        price = price.slice(0,1);

        if(price == "€")
        {
            area = "europe";
        }
        if(price == "£")
        {
            area = "europe";
        }
        if(price == "\$")
        {
            area == "northamerica";
        }
        if(price == "\¥")
        {
            area == "asia";
        }

欧元和英镑有效,但日元和美元无效。有人有想法吗?

I want to parse an an element and set the area by currency.

HTML:

<span id="price">¥82,84</span><br/>

Javascript:

        price = document.getElementById("price").innerHTML;
        price = price.slice(0,1);

        if(price == "€")
        {
            area = "europe";
        }
        if(price == "£")
        {
            area = "europe";
        }
        if(price == "\$")
        {
            area == "northamerica";
        }
        if(price == "\¥")
        {
            area == "asia";
        }

Euro and Pounds are working, but Yen and Dollar not. Does anyone have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被翻牌 2025-01-09 11:54:13

您在应使用赋值运算符 = 的语句中使用了相等比较 ==

You are using equality comparison == in the statements where you should use assignment operator =.

遗忘曾经 2025-01-09 11:54:13

.innerHTML 可能会返回 HTML 实体 $。请改用 .textContent.innerText。另外,由于 price 不能是两个不同的字符,因此我建议使用嵌套的 else if 而不是多个 if

错误本身位于美元和人民币区块:您使用的是 == 而不是普通的 =

    price = document.getElementById("price").textContent; // or innerText for IE
    price = price.charAt(0); // equal to .slice(0,1);

    if(price == "€") {
        area = "europe";
    } else if(price == "£") {
        area = "europe";
    } else if(price == "$") {
        area = "northamerica";
    } else if(price == "¥") {
        area = "asia";
    } else { // Default
        area = "Unknown";
    }

还有另外两种方法:

  • 切换块:

    开关(价格){
       案例 '€': 案例 '£':
           区域='欧洲';
       休息;
       案例“$”:
           区域 = '北美';
       休息;
       案例 '¥'
           区域='亚洲';
       休息;
       默认:
           面积='未知';
    }
    
  • 哈希:

    var Price = document.getElementById("price").textContent; //或内部文本(IE)
    价格=价格.charAt(0);
    var 价格到区域 = {
         '€': '欧洲',
         '£': '欧洲',
         '
    
: '北美', '¥': '亚洲' }; var 区域 = PriceToArea[价格] || '未知'; //默认未知

.innerHTML might return the HTML entities $. Use .textContent or .innerText instead. Also, since price cannot be two different chars, I suggest to use nested else ifs instead of multiple ifs.

The error itself is located at the dollar and yuan blocks: You are using == instead of a plain =.

    price = document.getElementById("price").textContent; // or innerText for IE
    price = price.charAt(0); // equal to .slice(0,1);

    if(price == "€") {
        area = "europe";
    } else if(price == "£") {
        area = "europe";
    } else if(price == "$") {
        area = "northamerica";
    } else if(price == "¥") {
        area = "asia";
    } else { // Default
        area = "Unknown";
    }

There are two other methods:

  • Switch blocks:

    switch(price) {
       case '€': case '£':
           area = 'europe';
       break;
       case '
    
  • Hashes:

    var price = document.getElementById("price").textContent; //or innerText (IE)
    price = price.charAt(0);
    var priceToArea = {
         '€': 'europe',
         '£': 'europe',
         '
    
: area = 'northamerica'; break; case '¥' area = 'asia'; break; default: area = 'unknown'; }
  • Hashes:

  • : 'northamerica', '¥': 'asia' }; var area = priceToArea[price] || 'Unknown'; //Default Unknown
    : area = 'northamerica'; break; case '¥' area = 'asia'; break; default: area = 'unknown'; }
  • Hashes:

  • 变身佩奇 2025-01-09 11:54:13

    除了 Rob W 的回答之外,为了清楚起见,我还将使用 switch 语句:

    switch (price)
    {
        case "€":
        case "£":
            area = "europe";
            break;
        case "$":
            area = "northamerica";
            break;
        case "¥":
            area = "asia";
            break;
    }
    

    Further to Rob W's answer I would also use a switch statement for clarity:

    switch (price)
    {
        case "€":
        case "£":
            area = "europe";
            break;
        case "$":
            area = "northamerica";
            break;
        case "¥":
            area = "asia";
            break;
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文