繁体   English   中英

如何过滤文本,以便将部分文本转换为货币?

[英]How do I filter text so I can convert part of it to a currency?

我有一些数字需要转换为货币。 我正在使用此脚本进行转换( https://github.com/CodersPress/jQuery-Currency ),该转换适用于我的一个号码,但不适用于另一个号码。 我不知道如何在li中定位数字(在这种情况下,我无法修改html输出)。 我试图过滤文本以从li中的数字重新定位“:”,但仍然无法使货币换算对li中的数字起作用。

我在这里有一个小提琴: https : //jsfiddle.net/dcrr5qxf/5/

这是我的html:

<ul class="cfeature">                              
    <li class="purchase_price"><label>Purchase Price</label>: 333333</li>
</ul>
<p class="purchase_price">
    <label>Purchase Price:&nbsp;</label>
    <strong><span>333333.00</span></strong>
</p>

这是我的js:

$('.purchase_price').each(function() {
    $(this).html($(this).html().replace(":", ""));
});
$('.purchase_price').each(function() {
    $(this).html($(this).html().replace("Purchase Price", "Purchase Price:"));
});

$(".purchase_price span").currency(); // this one works as the number is in a <span>
$(".purchase_price").currency(); // this one doesn't work
});

});

您可以解决遍历<li>的问题,并对每个问题执行以下3个步骤:

  1. 将数字包装在<div>并给它一个类,例如forCurrency
  2. .currency()应用于此<div>并获取相应的文本
  3. 将包装的<div>替换为我们刚得到的文本

jQuery的:

    $("ul.cfeature > li").each(function(){

        // wrap the number in a div
        $(this).html(function(_, html) {
            return html.replace(/(\d+)\s*$/, "<div id='forCurrency'>$1</div>");
        });

        // apply .currency() and get the corresponding text
        var currencyText = $("div#forCurrency").currency().text();
        console.log(currencyText);

        // replace the wrapped div with the text that we just got
        $("div#forCurrency").replaceWith(currencyText);

    });

更新了jsFiddle

暂无
暂无

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

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