html怎么让背景颜色百分比,jquery – CSS设置背景颜色只是表行宽度的一个百分比...

您可以使用线性梯度.

如果百分比是40%:

table{

background: -webkit-gradient(linear, left top, right top, color-stop(40%,#F00), color-stop(40%,#00F));

background: -moz-linear-gradient(left center, #F00 40%, #00F 40%);

background: -o-linear-gradient(left, #F00 40%, #00F 40%);

background: linear-gradient(to right, #F00 40%, #00F 40%);

}

所以,在JavaScript中,

var percentage=40,

col1="#F00",

col2="#00F";

var t=document.getElementById('table');

t.style.background = "-webkit-gradient(linear, left top,right top, color-stop("+percentage+"%,"+col1+"), color-stop("+percentage+"%,"+col2+"))";

t.style.background = "-moz-linear-gradient(left center,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";

t.style.background = "-o-linear-gradient(left,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";

t.style.background = "linear-gradient(to right,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";

如果要将其应用于每一行的方式不同:

var col1="#F00",

col2="#00F";

var els=document.getElementById('table').getElementsByTagName('tr');

for (var i=0; i

var percentage = Number(els[i].getElementsByTagName('td')[1].firstChild.nodeValue);

els[i].style.background = "-webkit-gradient(linear, left top,right top, color-stop("+percentage+"%,"+col1+"), color-stop("+percentage+"%,"+col2+"))";

els[i].style.background = "-moz-linear-gradient(left center,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)" ;

els[i].style.background = "-o-linear-gradient(left,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";

els[i].style.background = "linear-gradient(to right,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)" ;

}

问题是将背景设置为tr可以很好地在Firefox和Opera上运行,但是在Chrome上,渐变应用于每个单元格.

#table td {display: inline-block;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值