如何用原生JS编写示例代码实现货币汇率转换功能?

更新于
2026-09-25 12:46:44
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计184个文字,预计阅读时间需要1分钟。

如何用原生JS编写示例代码实现货币汇率转换功能?

Exchange Rate Calculator

代码如下

html代码

如何用原生JS编写示例代码实现货币汇率转换功能?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta api.exchangerate-api.com/v4/latest/${currency_one}`) .then(res => res.json()) .then(data => { const rate = data.rates[currency_two]; rateEl.innerText = `1 ${currency_one} = ${rate} ${currency_two}`; amountEl_two.value = (amountEl_one.value * rate).toFixed(2); }); }; // 得到汇率 function getData() { let currency_one = currencyEl_one.value; let currency_two = currencyEl_two.value; fetch(`api.exchangerate-api.com/v4/latest/${currency_one}`) .then(res => res.json()) .then(data => { const rate = data.rates[currency_two]; rateEl.innerText = `1 ${currency_one} = ${rate} ${currency_two}`; amountEl_two.value = (amountEl_one.value * rate).toFixed(2); }); }

运行效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计184个文字,预计阅读时间需要1分钟。

如何用原生JS编写示例代码实现货币汇率转换功能?

Exchange Rate Calculator

代码如下

html代码

如何用原生JS编写示例代码实现货币汇率转换功能?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta api.exchangerate-api.com/v4/latest/${currency_one}`) .then(res => res.json()) .then(data => { const rate = data.rates[currency_two]; rateEl.innerText = `1 ${currency_one} = ${rate} ${currency_two}`; amountEl_two.value = (amountEl_one.value * rate).toFixed(2); }); }; // 得到汇率 function getData() { let currency_one = currencyEl_one.value; let currency_two = currencyEl_two.value; fetch(`api.exchangerate-api.com/v4/latest/${currency_one}`) .then(res => res.json()) .then(data => { const rate = data.rates[currency_two]; rateEl.innerText = `1 ${currency_one} = ${rate} ${currency_two}`; amountEl_two.value = (amountEl_one.value * rate).toFixed(2); }); }

运行效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。