我在div中有一组这样的数字:<div id="numbers">1,2,3,4,5,6,7,8,9,10</div>
如何找到该组中的最高数字并显示它?
答案 0 :(得分:4)
//read content of div
var list = document.getElementById("numbers").innerText;
//split it into an array of numbers
list = list.split(",");
//get the biggest element
var max = Math.max.apply(null, list);
//ta-daaaa
document.write("The max is: " + max);
<div id="numbers">1,2,3,4,5,6,131,23,99,8,15</div>
应该这样做。
答案 1 :(得分:2)
您可以尝试这样的事情:
// get the numbers that are contained in your div
var numbers = document.getElementById("numbers").innerHTML;
// split the string you have read and create an array of numbers
var array = numbers.split(',').map(function(number){ return parseFloat(number)});
// sort that array
array = array.sort(function(a,b){ return b-a;});
// return the first element, which would be the highest.
alert("The greatest number is "+array[0]);
&#13;
<div id="numbers">1,2,3,4,5,6,7,8,9,10,105.20,110.85</div>
&#13;