计算最大值并将背景设置为相等的父级

Calculate max value and set background to equal parent

本文关键字:设置 最大值 背景 计算      更新时间:2023-09-26

>im现在尝试:

  1. 从文本中获取数字完成
  2. 将其设置为等于thisDONE
  3. 获取最大完成

4.并将背景parent最大值设置为例如background: #000;

所以我的问题是我将如何获得以前计算的 MAX 值的父级?

代码笔演示

$('.views').each(function(){
    valText = $(this).text();
    $(this).attr('value', valText);
});
var numbers = $(".views").map(function(){
    return parseFloat(this.getAttribute('value')) || -Infinity;
});
var calculate = Math.max.apply(Math, numbers);
$("#max").html(calculate);
<div><div class="views" >10</div></div>
<div><div class="views" >1</div></div>
<div><div class="views" >7</div></div>
<div><div class="views" >5</div></div>
<div><div class="views" >3</div></div>

最大值为:

<p>Max value is: <span id="max"></span></p>

有点棘手,将集合减少到里面数字最多的一个

var el = $('.views').toArray().reduce(function(a,b) {
    return (+$(a).text()) > (+$(b).text()) ? $(a) : $(b);
});

小提琴

要获得父母,只需

el.parent();

http://codepen.io/anon/pen/GoyzMd

var el = $('.views').toArray().reduce(function(a,b) {
    return (+$(a).text()) > (+$(b).text()) ? $(a) : $(b);
});
$("#max").html(el);
$("#max").parent().css({'background-color':'red'})

诸如此类?