當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript 四. Math对象的属性和方法
生活随笔
收集整理的這篇文章主要介紹了
JavaScript 四. Math对象的属性和方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <!-- 定時刷新網頁 -->
5 <!-- <meta http-equiv="refresh" content="1"> -->
6 <title></title>
7 <script type="text/javascript">
8 /*
9 Math對象
10 靜態(tài)對象, 不需要創(chuàng)建實例, 可以直接使用
11 */
12
13 // 圓周率
14 document.write(Math.PI);
15 document.write("<hr>");
16
17 // 絕對值
18 document.write(Math.abs(-9));
19 document.write("<hr>");
20
21 // 向上取整 去掉小數, 整數+1
22 document.write(Math.ceil(3.2));
23 document.write("<hr>");
24
25 // 向下取整 去掉小數
26 document.write(Math.floor(3.9));
27 document.write("<hr>");
28
29 // 四舍五入
30 document.write(Math.round(3.5));
31 document.write("<hr>");
32
33 document.write(Math.round(3.1));
34 document.write("<hr>");
35
36 // x的y次方
37 document.write(Math.pow(2, 24));
38 document.write("<hr>");
39
40 // 開方
41 document.write(Math.sqrt(16));
42 document.write("<hr>");
43
44 // 0 - 1 的隨機數
45 document.write(Math.random());
46 document.write("<hr>");
47
48 // 實例1: 兩個數之間的隨機整數
49 function getRandom(min, max) {
50
51 // 隨機整數
52 random = Math.random() * (max - min) + min;
53
54 // 向下取整
55 random = Math.floor(random);
56
57 // 返回隨機整數
58 return random;
59 }
60
61 // 調用函數
62 document.write(getRandom(0, 10));
63 document.write("<hr>");
64
65 // 實例2: 隨機網頁背景色
66
67 // 得到隨機顏色值
68 // (1) 三個隨機數并轉換為16進制
69 var red = getRandom(0, 256).toString(16);
70 var green = getRandom(0, 256).toString(16);
71 var blue = getRandom(0, 256).toString(16);
72 var color = "#" + red + green + blue;
73 // document.write(red + " ");
74 // document.write(green + " ");
75 // document.write(blue + " ");
76
77 // (2) 寫CSS樣式
78 var str = "";
79 str += '<style type="text/css">';
80 str += 'body {';
81 str += ' background-color:' + color + ';';
82 str += '}';
83 str += '</style>';
84
85 // 將CSS樣式寫入網頁
86 // document.write(str);
87
88 // 第二種方式
89 // (1) 得到隨機顏色值
90 var color = "#" + (getRandom(0, Math.pow(2, 24)).toString(16));
91
92 /* (2) 設置網頁背景色
93 document: 網頁對象
94 body: 子對象
95 bgColor: body的屬性
96 除了body以外, 其它標記必須使用id來訪問
97 */
98 document.body.bgColor = color;
99 </script>
100 </head>
101 <body>
102
103 </body>
104 </html>
?
轉載于:https://www.cnblogs.com/ZeroHour/p/6364628.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的JavaScript 四. Math对象的属性和方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CVE-2017-5521: Bypas
- 下一篇: 构建之法第三章