通过js判断,数字的正负,来改变颜色,下面的100为什么总是显示红色,-1不变色?,怎样判断多个数字
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function change(){
var num=document.getElementById("num");
var col=Number(num)>"0"?"green":"red";
num.style.color=col;
};
</script>
</head>
<body onload="change();">
<div id="num" style="font-weight:bold;">100</div>
<div id="num">-1</div>
</body>
</html> 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function change(){
var num=document.getElementById("num");
var col=Number(num)>"0"?"green":"red";
num.style.color=col;
};
</script>
</head>
<body onload="change();">
<div id="num" style="font-weight:bold;">100</div>
<div id="num">-1</div>
</body>
</html> 展开
2个回答
推荐于2018-05-07
展开全部
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>main.html</title>
<style type="text/css">
</style>
<script type="text/javascript">
function change ()
{
var divs = document.getElementsByTagName ("div");
for ( var i = 0; i < divs.length; i++)
{
var obj = divs[i];
var num = parseFloat (obj.firstChild.nodeValue);
obj.style.color = num > 0 ? "green" : "red";
}
};
</script>
</head>
<body onload="change();">
<div>100</div>
<div>-1</div>
<div>1</div>
<div>-11</div>
</body>
</html>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询