我遇到了以下JSFiddle问题。最大值+选择Div
我基本上想要用黄色边框突出显示最高值的div。
但是,如果多个div具有相同的max值,那么程序必须从最大值的所有div中随机选择一个div。
例如,三个div中的一个应该是随机选择的,它的边框颜色应该是黄色的。
HTML
<div class="num">16</div>
<div class="num">20</div>
<div class="num">18</div>
<div class="num">20</div>
<div class="num">20</div>
CSS
.num {
border: 2px solid green;
width:120px;
text-align: center;
}
JS
var max = -Infinity;
var maxEle = $(".num").each(function() {
if (+this.value > max) max = +this.value;
}).filter(function() {
return this.value == max;
});
if (maxEle.length > 1) maxEle = maxEle.eq(Math.floor(Math.random() * maxEle.length));
maxEle.css("border-color", "yellow");
您的代码并不突出,最大值(一个或多个)的股利?对? – undefined
那么我的代码应该是,但它不工作。它应该将边框颜色更改为最大值为div的黄色(突出显示) – JoseBazBaz
也可以查看[Math.max](https://developer.mozilla.org/en-US/docs/JavaScript/参考/ Global_Objects/Math/max) – mplungjan