Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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:更改动态api数据的颜色?_Javascript_Html_Css - Fatal编程技术网

Javascript:更改动态api数据的颜色?

Javascript:更改动态api数据的颜色?,javascript,html,css,Javascript,Html,Css,我是HTML/Javascript的初学者,我需要一些帮助来制作我的第一个价格代码。 到目前为止,我已经设法从Coinmarketcap的API获取并显示数据,但我想添加一些功能 如何创建一个函数,根据价格变化值是正是负来改变其颜色 绿色表示正极,红色表示负极 函数getElementid{ return document.getElementByIdid; } 取回https://api.coinmarketcap.com/v2/ticker/1697/' .thenres=>res.jso

我是HTML/Javascript的初学者,我需要一些帮助来制作我的第一个价格代码。 到目前为止,我已经设法从Coinmarketcap的API获取并显示数据,但我想添加一些功能

如何创建一个函数,根据价格变化值是正是负来改变其颜色 绿色表示正极,红色表示负极

函数getElementid{ return document.getElementByIdid; } 取回https://api.coinmarketcap.com/v2/ticker/1697/' .thenres=>res.json .thenres=>{ 常数数据=分辨率数据; getElement'usdprice'。innerHTML=$+data.quotes.USD.price; getElement'change'.innerHTML=data.quotes.USD.percent_change_24h+%; }; 桌子{ 边界间距:2米; 文本对齐:居中; } 美元价格{ 颜色:白色; 背景色:黑色; 字体大小:200%; 字体大小:粗体; } 改变{ 颜色:白色; 背景色:黑色; 字体大小:200%; 字体大小:粗体; }
一种方法是根据价格向表单元格中添加一个额外的类名

const data = res.data;
getElement('usdprice').innerHTML = "$" + data.quotes.USD.price;
getElement('change').innerHTML = data.quotes.USD.percent_change_24h + "%";

if(data.quotes.USD.price < 0)
    getElement('usdprice').className = "negative"; // If price is negative add the 'negative' class
像这样:

函数getElementid{ return document.getElementByIdid; } 取回https://api.coinmarketcap.com/v2/ticker/1697/' .thenres=>res.json .thenres=>{ 常数数据=分辨率数据; getElement'usdprice'。innerHTML=$+data.quotes.USD.price; getElement'change'.innerHTML=data.quotes.USD.percent_change_24h+%; getElement'change'.classList.删除'plus','减号' getElement'change'.classList.add data.quotes.USD.percent_change_24小时>0?'plus':'减号'; }; 桌子{ 边界间距:2米; 文本对齐:居中; } 美元价格{ 颜色:白色; 背景色:黑色; 字体大小:200%; 字体大小:粗体; } 改变{ //颜色:白色; 背景色:黑色; 字体大小:200%; 字体大小:粗体; } .另外{ 颜色:绿色; } .减去{ 颜色:红色; }
虽然我不确定您是想更改usdprice单元格的颜色还是更改单元格的颜色,但我认为以下代码将帮助您:

function getElement(id) {
  return document.getElementById(id);
}

fetch('https://api.coinmarketcap.com/v2/ticker/1697/')
.then(res => res.json())
.then((res) => {
  const data = res.data;
  document.getElementById('usdprice').innerHTML = "$" + data.quotes.USD.price;
  document.getElementById('change').innerHTML = data.quotes.USD.percent_change_24h + "%";

  if(data.quotes.USD.price >= 0) {
    document.getElementById('usdprice').style.color = 'green';
  } else {
    document.getElementById('usdprice').style.color = 'red';
  }

});

你失踪了谢谢你的帮助谢谢你的帮助很高兴能帮上忙
function getElement(id) {
  return document.getElementById(id);
}

fetch('https://api.coinmarketcap.com/v2/ticker/1697/')
.then(res => res.json())
.then((res) => {
  const data = res.data;
  document.getElementById('usdprice').innerHTML = "$" + data.quotes.USD.price;
  document.getElementById('change').innerHTML = data.quotes.USD.percent_change_24h + "%";

  if(data.quotes.USD.price >= 0) {
    document.getElementById('usdprice').style.color = 'green';
  } else {
    document.getElementById('usdprice').style.color = 'red';
  }

});