gpt4 book ai didi

javascript - 从表格单元格中获取最高数字

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:23:47 25 4
gpt4 key购买 nike

我正在尝试从具有特定类别的表格单元格中获取最大数字 (122,28.22)。

这是我的 HTML 代码:

<table>
<tr>
<td class="id">3.2091</td>
</tr>
<tr>
<td class="id">122,28.22</td>
</tr>
<tr>
<td class="id">4.8271</td>
</tr>
</table>

这确实是我尝试过的方法,但它只返回 NaN。所以 math.max 对它不起作用。

var high = Math.max.apply(Math, $('.id').map(function(){
return $(this).text()
}))

alert(high);

最佳答案

你得到 NaN作为你的号码 122,28.22包含 ,因此您可以使用 replace 将其更改为有效的数字格式

function findMax(){
let td = document.getElementsByClassName('id')
let value = [...td].map(e=> +e.innerText.replace(/,/,''))
console.log(Math.max(...value))
}
<table>
<tr>
<td class="id">3.2091</td>
</tr>
<tr>
<td class="id">122,28.22</td>
</tr>
<tr>
<td class="id">4.8271</td>
</tr>
</table>

<button onClick="findMax()">Find max </button>

关于javascript - 从表格单元格中获取最高数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55448772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com