源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p>toString()方法可以输出以16为基数(16进制), 以8为基数 (8进制), 以2为基数 (2进制).</p> <p id="demo"></p> <button onclick="myFunction()">试试</button> <script> function myFunction() { var myNumber = 128; document.getElementById("demo").innerHTML = "128 = " + myNumber + " 十进制, " + myNumber.toString(16) + " 十六进制, " + myNumber.toString(8) + " 八进制, " + myNumber.toString(2) + " 二进制." } </script> </body> </html>
运行结果