5、数据相加赋值相等克隆、字符串|数组|对象之为空|含有|位置|项|遍历、字符串|正则|数组|对象之方法、递归(虚拟DOM、快速排序、react框架)、两行布局、两列布局、三列布局、五种盒子居中、em与rem自适应、Flex布局、多图片延迟加载下载、canvas绘图之党徽|五角星|时钟、弹窗之内置|拖拽缩放、表格排序、拖拽之普通|zTree、设计模式(3000行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 | 一、数据相加赋值相等克隆 来源,https: //blog.csdn.net/yyh1994/article/details/129316857 1、小数相加出错 (1)现象,console.log( 0.1+0.2 ); //0.30000000000000004,3和4之间有15个0 (2)原因 A、人用十进制运算 B、计算机把人的十进制转化成自身的二进制,然后进行运算 C、在JS中,遵循IEEE754标准(有3个E),使用双精度浮点数( double )64位(8字节)来存储一个浮点数 (3)十进制转化为二进制--整数 A、十进制数除以2,得到商和余数,余数为二进制数的最低位 B、商除以2,得到商和余数,余数为二进制数的次低位 C、以此类推,直到商为0时为止,0为二进制数的最高位 (4)十进制转化为二进制--小数 A、将十进制小数乘以2,取出结果的整数部分,为小数部分的最高位 B、将得到的小数部分再次乘以2,取出结果的整数部分,为小数部分的次高位 C、直到小数部分为零或者达到所需的精度 (5)将0.875转换为二进制小数,过程如下 A、0.875乘以2得到1.75,取整数部分1,为小数部分的最高位 B、0.75乘以2得到1.5,取整数部分1,为小数部分的次高位 C、0.5乘以2得到1.0,取整数部分1 D、0.875的二进制表示为0.1110 2、i=i+1,编程理解与数学理解 (1)编程理解,赋值,把右边的计算结果赋值给左边的变量 (2)数学理解,移项,合并同类项0=1 3、数据相等的判断方法 // 封装一个判断两个js数据是否相等的方法 //(1)方法1 function isDeepEqualA(aaa, bbb) { if ( typeof aaa !== typeof bbb || Array.isArray(aaa) !== Array.isArray(bbb)) { //以下,A、数据类型不同;B、数据类型相同,且1个是数组,1个不是数组(是对象) return false ; } else if (Array.isArray(aaa)) { //以下,2个同为数组 for ( let i = 0; i < aaa.length; i++) { if (!isDeepEqualA(aaa[i], bbb[i])) { return false ; } } } else if ( typeof aaa === 'object' && aaa !== null && bbb !== null ) { //以下,2个都为对象(都不为null) const aaaKeys = Object.keys(aaa).sort(); const bbbKeys = Object.keys(bbb).sort(); if (JSON.stringify(aaaKeys) !== JSON.stringify(bbbKeys)) { return false ; } for ( let key of aaaKeys) { if (!isDeepEqualA(aaa[key], bbb[key])) { return false ; } } } else { //以下,A、2个同为基本类型;B、2个同为对象,且1个为null,1个(不)为null return aaa === bbb; } return true ; } //(2)方法2 function isDeepEqualB(aaa, bbb) { if (Object.prototype.toString.call(aaa) !== Object.prototype.toString.call(bbb)) { //以下,2个不同类型的数据 return false ; } else if (Object.prototype.toString.call(aaa) === "[object Array]" ) { //以下,2个同为数组类型的数据 for ( let i = 0; i < aaa.length; i++) { if (!isDeepEqualB(aaa[i], bbb[i])) { return false ; } } } else if (Object.prototype.toString.call(aaa) === "[object Object]" ) { //以下,2个同为对象类型的数据 const aaaKeys = Object.keys(aaa).sort(); const bbbKeys = Object.keys(bbb).sort(); if (JSON.stringify(aaaKeys) !== JSON.stringify(bbbKeys)) { return false ; } for ( let key of aaaKeys) { if (!isDeepEqualB(aaa[key], bbb[key])) { return false ; } } } else { //以下,2个同为基本类型或同为null的数据 return aaa === bbb; } return true ; } //(3)数据 if (1 == 1){ console.log( '正在使用A组数据' ); var aaa = [ 0, 1, 2, 3 ]; var bbb = { 0:0, 1:1, 2:2, 3:3 }; } else { console.log( '正在使用B组数据' ); var aaa = { a: 1, b: 'b' , c: false , d: null , }; var bbb = { a: 1, b: 'b' , c: false , d: null , e: undefined}; } //(4)比较 console.log( '用,isDeepEqualA,比较' ,isDeepEqualA(aaa, bbb)); //false console.log( '用,isDeepEqualB,比较' ,isDeepEqualB(aaa, bbb)); //false 4、深克隆 //(1)方法1,value为undefined的键值对将消失 function deepCloneA(arrayOrObject) { return JSON.parse(JSON.stringify(arrayOrObject)); } //(2)方法2 function deepCloneB(arrayOrObject) { var target = null ; //{ }.toString.call也可以用Object.prototype.toString.call代替 if ({}.toString.call(arrayOrObject) === "[object Array]" ) target = []; if ({}.toString.call(arrayOrObject) === "[object Object]" ) target = {}; for ( var key in arrayOrObject) { var value = arrayOrObject[key]; if ({}.toString.call(value) === "[object Array]" || {}.toString.call(value) === "[object Object]" ) { target[key] = deepCloneB(value); } else { target[key] = value; } } return target; } //(3)方法3,来源,https://chat.deepseek.com/,用js写一个深克隆函数 function deepCloneC(obj) { if ( typeof obj !== 'object' || obj === null ) { // 如果不是对象或者是 null,直接返回 return obj; } if (Array.isArray(obj)) { // 处理数组 const arrCopy = []; for ( let i = 0; i < obj.length; i++) { arrCopy[i] = deepCloneC(obj[i]); } return arrCopy; } const objCopy = {}; // 处理对象 for ( const key in obj) { if (obj.hasOwnProperty(key)) { objCopy[key] = deepCloneC(obj[key]); } } return objCopy; } //(4)数据 var aaa = { a: 1, b: 'b' , c: false , d: null , e: undefined}; var bbb = { a: 1, b: { c: 3 }, d: { e: [1, 2] } }; //(5)克隆 console.log( '用,deepCloneA,JSON简易克隆' ); console.log(deepCloneA(aaa)); console.log(deepCloneA(bbb)); console.log( '用,deepCloneB,不区分数据类型克隆' ); console.log(deepCloneB(aaa)); console.log(deepCloneB(bbb)); console.log( '用,deepCloneC,区分数据类型克隆' ); console.log(deepCloneC(aaa)); console.log(deepCloneC(bbb)); 二、字符串、数组、对象之为空、含有、位置、项、遍历 1、字符串 (1)为空的判断方法 A、 if (!str.trim().length) //trim,修剪 B、 if (/^\s*$/.test(str)) //只有0到多个空格时,为true (2)含有“某个字符”的判断方法, A、 if (str.indexOf(item) != -1) (3)获取item的位置,str.indexOf(item) (4)获取index的项 A、str.charAt(index) B、str[index] (5)遍历的方法, for , for … in , for …of, var str = 'abc' ; for ( var i = 0; i < str.length; i++) { //可以没有var console.log( 'for循环' , str[i] ); } for ( var index in str){ //可以没有var console.log( 'for-in循环' , index,str[index] ); } for ( var item of str){ //可以没有var console.log( 'for-of循环' , item ); } 2、数组 (1)为空的判断方法 A、 if (!ary.length) //==长度判断法, B、 if (JSON.stringify(ary)== "[]" ) //==字符串判断法,空数组里有空格,字符串里没有空格,结果也是true (2)含有“某个项”的判断方法 A、 if (ary.indexOf(item) != -1) //首选方案 B、 if (ary.includes(item)) (3)获取item的位置,ary.indexOf(item) (4)获取index的项,ary[index] (5)遍历的方法, for , for … in , for …of,另有forEach、map var array = [ 'a' , 'b' ]; for ( var i = 0; i < array.length; i++) { //可以没有var console.log( 'for循环' , array[i] ); } for ( var index in array){ //可以没有var console.log( 'for-in循环' , index,array[index] ); } for ( var item of array){ //可以没有var console.log( 'for-of循环' , item ); } // const person = { // name: 'Alice', // age: 30, // }; // const entries = Object.entries(person); //返回一个数组,其中每个元素都是一个包含对象属性名和属性值的数组 // console.log(entries); // for (const [key, value] of entries) { // console.log( key, value ); // } 3、对象 附、js对象之属性获取说明 A、只能获取自身属性:Object.keys(仅含可枚举属性)、Object.getOwnPropertyNames(还含不可枚举属性)、Object.hasOwnProperty(还含不可枚举属性) B、获取自身属性、原型链属性:打点、 for ... in 、 in 附、js对象之可枚举属性说明 A、可以通过某些方法(如 for ... in 循环、Object.keys())被遍历或访问的属性 B、由描述符(enumerable)决定的,默认为 true C、自身属性、原型链属性有的可枚举,有的不可枚举 (1)为空的判断方法 A、 if (!Object.keys(obj).length) //==长度判断法1/2 B、 if (!Object.getOwnPropertyNames(obj).length) //==长度判断法2/2 C、 if (JSON.stringify(obj)== "{}" ) //==字符串判断法,空对象里有空格,字符串里没有空格,结果也是true (2)含有“某个键”的判断方法 A、 if (key in obj) //------等同于if(Reflect.has(obj, name)) B、 if (obj.hasOwnProperty(key)) 注意, if (obj.key)不可以,因为排除了obj={ key: 0 || false || null || undefined } (3)获取key属性项的位置 A、对象里面的项是无序的,如果有位置要求,则应该写成数组形式 (4)获取index位置的项 A、对象里面的项是无序的,如果有位置要求,则应该写成数组形式 (5)遍历的方法, for … in var object = { a: 1, b: 2 }; for ( var key in object ){ //可以没有var console.log( object [key] ); } 三、字符串方法(字符串的方法):所有方法均不改变原字符串 菜鸟教程,https: //www.runoob.com/js/js-strings.html MDN WEB,https: //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String 注意,数组也有的方法,如concat、includes、indexOf、slice 1、charAt:通过索引获取字符,不存在时,返回空字符串。也可以用str[0] 2、charCodeAt:通过索引获取字符的unicode值 3、concat:把多个字符串拼接成一个字符串,相当于加号 4、includes (1)作用:判断字符串中是否有另一字符串 (2)参数:要判断的字符串 (3)返回值:boolean (4)是否改变原字符串:不变 var pets = 'catdogbat' ; console.log(pets.includes( 'cat' )); //true 5、indexOf:通过字符获取(该字符首次出现时所在位置的)索引,不存在时,返回-1。 console.log( '20000' > '000' ); //true,前者是否包含后者,且不等于后者,又例location>"https" console.log( '000' < '20000' ); //true,后者是否包含前者,且不等于前者 6、localeCompare,在当前语言环境中,比较两个字符串的前后(先比较第1个字符,相同则比较第2个字符) (1)参数:字符串 (2)返回值:-1,0,1,表示(前者在后者)之前、相等、之后 console.log( 'a' .localeCompare( 'b' ) ); (3)示例,字符串排序,借用数组的sort方法 var str = 'SSSadsdcc1CCC荣的Adkdk辱kkkDc观c4dddaaa发货ccCCs6SSSSSs' ; str = str.split( '' ).sort(function (one, two) { return one.localeCompare(two) //升序,反之为降序 }). join ( '' ); console.log(str); 7、match,搜索一个字符串-能够-匹配某个模式-的字符,与正则中的exec相似 (1)参数是正则,不加g,匹配一次;返回值为数组,[大正则第1次匹配到的内容,第1、2、3...个小分组匹配到的内容,索引,整个字符串,groups] var reg=/(\d)A(\d)/; var before= "000002223A45556A7888" ; var after=before.match(reg); console.log( '返回数组:' ,after); // [ '3A4', '3', '4', index: 8, input: '000002223A45556A7888', groups: undefined ] (2)参数是正则,加g,匹配多次;返回值为数组,[大正则第1次匹配到的内容,大正则第2次匹配到的内容,大正则第3次匹配到的内容,...] var reg=/(\d)A(\d)/g; var before= "000002223A45556A7888" ; var after=before.match(reg); console.log( '返回数组:' ,after); //[ '3A4', '6A7' ] (3)参数是字符串;返回值为数组,和不加g的正则一样 const str = 'hello world' ; const result1 = str.match(/world/); const result2 = str.match( 'world' ); console.log(result1); //[ 'world', index: 6, input: 'hello world', groups: undefined ] console.log(result2); //[ 'world', index: 6, input: 'hello world', groups: undefined ] console.log(result2.index); //6 8、matchAll,搜索一个字符串-能够-匹配某个模式-的所有字符 var str = 'test-test2' ; var regexp = /test(\d?)/g; var array = [...str.matchAll(regexp)]; console.log(array); 9、slice:复制字符串 (1)没有参数时,返回整个字符串 (2)一个参数时,返回参数到末尾的字符串 (3)两个参数时(m,n),返回从索引m到索引n,不包括n 另,console.log( '7654321' .slice(-3) ); //从后往前取3项,构成的新字符串 10、split:把字符串按照指定的分隔符拆分成数组(与数组里 join 方法相反) console.log( "ABCDEFG" .split( "" )); //[ 'A', 'B', 'C', 'D', 'E', 'F', 'G' ] console.log( "ABCDEFG" .split( "D" )); //[ 'ABC', 'EFG' ] 11、substr:复制字符串(弃用) (1)没有参数时,返回整个字符串 (2)一个参数时,返回返回参数到末尾的字符串 (3)两个参数时(m,n),返回从索引m开始的n个字符 12、substring:复制字符串 (1)没有参数时,返回整个字符串 (2)一个参数时,返回返回参数到末尾的字符串 (3)两个参数时(m,n),返回索引m到索引n,不包括n 13、trim:去字符串前后空格 var str= " hello world " ; console.log( '(' +str+ ')' ); console.log( '(' +str.trim()+ ')' ); 14、replaceAll:原字符串不变,返回值为新字符串 var before = "aba-aba-aba-aba-aba-aba-aba" ; var after = before.replaceAll( 'a' , "b" ) console.log( before ); //aba-aba-aba-aba-aba-aba-aba console.log( after ); //bbb-bbb-bbb-bbb-bbb-bbb-bbb 15、replace:原字符串不变,返回值为新字符串 (1)参数是“旧字符串+新字符串”时,旧字符串被新字符串取代 var before= 'old--CCC--old' ; var after=before.replace( "old" , "new" ); console.log( '返回字串:' ,after); //new--CCC--old (2)参数是“正则+新字符串”时, A、不加g,匹配一次 var reg=/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/; var before= 'AAA_12345678_BBB_87654321_CCC' ; var after=before.replace(reg, "0000000" ); console.log( '返回字串:' ,after); //AAA_0000000_BBB_87654321_CCC B、加g,匹配多次 var reg=/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/g; var before= 'AAA_12345678_BBB_87654321_CCC' ; var after=before.replace(reg, "0000000" ); console.log( '返回字串:' ,after); //AAA_0000000_BBB_0000000_CCC (3)正则+回调函数作参数时, A、不加g,匹配一次 var reg=/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/; var before= 'AAA_12345678_BBB_87654321_CCC' ; var i=0; var after=before.replace(reg,function(totalRegexp,A1,A2,A3,A4,A5,A6,A7,A8,firstIndex,totalString){ i=i+1; console.log( '"' +totalRegexp+ '"是被大正则第1次匹配到的内容' ); console.log( '"' +A1+ '"是被第1个小正1则匹配上的内容' ); console.log( '"' +A2+ '"是被第2个小正1则匹配上的内容' ); console.log( '"' +A3+ '"是被第3个小正1则匹配上的内容' ); console.log( '"' +A4+ '"是被第4个小正1则匹配上的内容' ); console.log( '"' +A5+ '"是被第5个小正1则匹配上的内容' ); console.log( '"' +A6+ '"是被第6个小正1则匹配上的内容' ); console.log( '"' +A7+ '"是被第7个小正1则匹配上的内容' ); console.log( '"' +A8+ '"是被第8个小正1则匹配上的内容,以此类推' ); console.log( '"' +firstIndex+ '"是本次(大正1则匹配到的)字符串的第1个字符,在整个字符串的索引' ); console.log( '"' +totalString+ '"是整个字符串,本轮匹配结束' ); console.log( "==========" ); return '(我将取代被大正1则第' +i+ '次匹配到的内容)' }); console.log( '返回字串:' ,after); // 返回值为基于原字符串的新字符串,用函数第1次返回的内容-代替-原字符串第1次被大正则匹配到的内容; // "12345678"是被大正1则第1次匹配到的内容 // "1"是被第1个小正1则匹配上的内容 // "2"是被第2个小正1则匹配上的内容 // "3"是被第3个小正1则匹配上的内容 // "4"是被第4个小正1则匹配上的内容 // "5"是被第5个小正1则匹配上的内容 // "6"是被第6个小正1则匹配上的内容 // "7"是被第7个小正1则匹配上的内容 // "8"是被第8个小正1则匹配上的内容,以此类推 // "4"是本次(大正1则匹配到的)字符串的第1个字符,在整个字符串的索引 // "AAA_12345678_BBB_87654321_CCC"是整个字符串,本轮匹配结束 // ========== // 返回字串: AAA_(我将取代被大正1则第1次匹配到的内容)_BBB_87654321_CCC B、加g,匹配多次 var reg=/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/g; var before= 'AAA_12345678_BBB_87654321_CCC' ; var i=0; var after=before.replace(reg,function(totalRegexp,A1,A2,A3,A4,A5,A6,A7,A8,firstIndex,totalString){ i=i+1; console.log( '"' +totalRegexp+ '"是被大正则第' +i+ '次匹配到的内容' ); console.log( '"' +A1+ '"是被第1个小正1则匹配上的内容' ); console.log( '"' +A2+ '"是被第2个小正1则匹配上的内容' ); console.log( '"' +A3+ '"是被第3个小正1则匹配上的内容' ); console.log( '"' +A4+ '"是被第4个小正1则匹配上的内容' ); console.log( '"' +A5+ '"是被第5个小正1则匹配上的内容' ); console.log( '"' +A6+ '"是被第6个小正1则匹配上的内容' ); console.log( '"' +A7+ '"是被第7个小正1则匹配上的内容' ); console.log( '"' +A8+ '"是被第8个小正1则匹配上的内容,以此类推' ); console.log( '"' +firstIndex+ '"是本次(大正1则匹配到的)字符串的第1个字符,在整个字符串的索引' ); console.log( '"' +totalString+ '"是整个字符串,本轮匹配结束' ); console.log( "==========" ); return '(我将取代第' +i+ '次被大正1则匹配到的内容)' }); console.log( '返回字串:' ,after); // 返回值为基于原字符串的新字符串,用函数第1、2、3...次返回的内容-代替-原字符串第1、2、3...次被大正则匹配到的内容; // "12345678"是被大正1则第1次匹配到的内容 // "1"是被第1个小正1则匹配上的内容 // "2"是被第2个小正1则匹配上的内容 // "3"是被第3个小正1则匹配上的内容 // "4"是被第4个小正1则匹配上的内容 // "5"是被第5个小正1则匹配上的内容 // "6"是被第6个小正1则匹配上的内容 // "7"是被第7个小正1则匹配上的内容 // "8"是被第8个小正1则匹配上的内容,以此类推 // "4"是本次(大正1则匹配到的)字符串的第1个字符,在整个字符串的索引 // "AAA_12345678_BBB_87654321_CCC"是整个字符串,本轮匹配结束 // ========== // "87654321"是被大正1则第2次匹配到的内容 // "8"是被第1个小正1则匹配上的内容 // "7"是被第2个小正1则匹配上的内容 // "6"是被第3个小正1则匹配上的内容 // "5"是被第4个小正1则匹配上的内容 // "4"是被第5个小正1则匹配上的内容 // "3"是被第6个小正1则匹配上的内容 // "2"是被第7个小正1则匹配上的内容 // "1"是被第8个小正1则匹配上的内容,以此类推 // "17"是本次(大正1则匹配到的)字符串的第1个字符,在整个字符串的索引 // "AAA_12345678_BBB_87654321_CCC"是整个字符串,本轮匹配结束 // ========== // 返回字串: AAA_(我将取代第1次被大正1则匹配到的内容)_BBB_(我将取代第2次被大正1则匹配到的内容)_CCC C、罕见用法 var reg=/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/; var before= 'AAA_12345678_BBB_87654321_CCC' ; var after=before.replace(reg, '$1' ); //返回值为基于原字符串的新字符串,用第1个小分组的内容-代替-原字符串第1次被大正1则匹配到的内容; console.log( '返回字串:' ,after); // AAA_1_BBB_87654321_CCC 16、字符串里字符重复次数 (1)对象法:把一个字母作为对象的属性名,字母出现的次数作为属性值;进而比较属性值的大小。 var str = 'SSSadsdccCCCAdkdkkkkDccdddaaaccCCsSSSSSs' ; function maxstr(str) { var obj = {}; var maxNum = 0; var maxVal = "" ; for ( var i = 0; i < str.length; i++) { var strNum = str.charAt(i).toLowerCase(); if (obj[strNum]) { obj[strNum]++; } else { obj[strNum] = 1; } } for ( var attr in obj) { if (obj[attr] > maxNum) { maxNum = obj[attr]; maxVal = attr; } else if (obj[attr] == maxNum) { maxVal += attr } } return "出现次数最多的字母是:" + maxVal + ";次数是:" + maxNum } var valNum = maxstr(str); console.log(valNum); (2)排序法:先对它们进行排序,再取出小分组和小分组被匹配的次数。 var str = 'SS张SadsdccCCC四Adkdkk张kkDccdddaa三acc四CCsS李SSSSs' ; function maxstr(str) { var maxNum = 0; var maxVal = "" ; str = str.split( '' ).sort(function (one, two) { return one.localeCompare(two) }). join ( '' ); console.log( "排序后的字符串是:" + str); //下面正则的意思是:"(\w)"字符、"\1"第1个括号、"+"1次到多次、"g全局"、"i"不区分大小写 str.replace(/(\w)\1+/gi, function (bigReg,item) { if (bigReg.length > maxNum) { maxNum = bigReg.length; maxVal = item; } else if (bigReg.length == maxNum) { maxVal += item; } }); return "出现次数最多的字母是:" + maxVal + ",次数" + maxNum; } var valNum = maxstr(str); console.log(valNum); 四、正则 附、修饰符 (1)g,使用全局匹配 (2)i,忽略大小写 附、正则实例获取 (1)console.log(/ab+c/i); // /ab+c/i (2)console.log( new RegExp( "ab+c" , "i" )); // /ab+c/i (3)console.log( new RegExp(/ab+c/, "i" )); // /ab+c/i 1、test,检测一个字符串-是否-匹配某个模式 var before= '程序员:123' ; var reg=/\d/; var after=reg.test(before); console.log( '返回布尔值:' ,after); 2、exec,搜索一个字符串-能够-匹配某个模式-的字符,与字符串中的match相似 (1)不加g,匹配一次;返回值为数组,[大正则第1次匹配到的内容,第1、2、3...个小分组匹配到的内容,索引,整个字符串,groups] var reg=/(\d)A(\d)/; var before= "000002223A45556A7888" ; var after=reg.exec(before); console.log( '返回数组:' ,after); console.log( '索引:' ,after.index); (2)加g,匹配多次;返回值为数组,[大正则第1次匹配到的内容,大正则第2次匹配到的内容,大正则第3次匹配到的内容,...] var reg=/(\d)A(\d)/g; var before= "000002223A45556A7888" ; var after=reg.exec(before); console.log( '返回数组:' ,after); 3、正则元字符 (1)\w:匹配数字、字母、下划线 (2)\W:匹配非数字、字母、下划线 (3)\n:匹配换行符 (4).:匹配非换行符 (5)\s:匹配空格 (6)\S:匹配非空格 (7)^:匹配输入字符串的开始位置;在方括号中使用时,表示排除方括号中的所有字符 (8)$:匹配输入字符串的结尾位置 (9)*:匹配前面的子表达式0-n次 (10)+:匹配前面的子表达式1-n次 (11)?:匹配前面的子表达式0或1次 (12)\r:匹配回车 (13)正则元字符的使用:MAC var MACMatch=/^[A-Fa-f0-9]{2}(:[A-Fa-f0-9]{2}){5}$|^[A-Fa-f0-9]{2}(-[A-Fa-f0-9]{2}){5}$/, 4、用正则封装解析方法(解析URL中的问号参数值以及HASH值) function queryUrlParam1(url) { var obj = {}; var reg1 = /([^?=&#]+)=([^?=&#]+)/g; var reg2 = /#([^?=&#]+)/; url.replace(reg1, function (keyValue,key,value) { obj[key] = value; }); if (reg2.test(url)) { obj[ 'HASH' ] = reg2.exec(url)[1]; } return obj; } function queryUrlParam2(url) { var obj = {}; var askText = '' ; var hashText = '' ; var askIndex = url.indexOf( '?' ) === -1 ? url.length : url.indexOf( '?' ); var hashIndex = url.indexOf( '#' ) === -1 ? url.length : url.indexOf( '#' ); if (askIndex < hashIndex) { askText = url.substring(askIndex + 1, hashIndex); hashText = url.substring(hashIndex + 1); } else { askText = url.substring(askIndex + 1); hashText = url.substring(hashIndex + 1, askIndex); } if (askText) { askText.split( '&' ).forEach(function(item){ let [key, value] = item.split( '=' ); obj[key] = value; }); } if (hashText) obj[ 'HASH' ] = hashText; return obj; }; var str = "https://www.baidu.com/newspage/data/landingsuper?AAA=1111&BBB=222&CCC=333#1234" console.log(queryUrlParam1(str)); console.log(queryUrlParam2(str)); var baidu = queryUrlParam1( "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=%E5%9C%B0%E5%9D%80%E6%A0%8F%20js%E4%BA%92%E8%BD%AC&rn=50&oq=%25E5%259C%25B0%25E5%259D%2580%25E6%25A0%258F%25E7%25BC%2596%25E7%25A0%2581%25E4%25B8%258E%25E6%25B1%2589%25E5%25AD%2597%2520js%25E4%25BA%2592%25E8%25BD%25AC&rsv_pq=d283c8db0000cb86&rsv_t=c351Ro%2FA%2FmhiIiW6LtAjCUz3gfW%2FzY%2FIdrQZsY%2BfrFrUBGGwf1OSd4mVEUY&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_btype=t&inputT=2982&rsv_sug3=7&rsv_sug1=7&rsv_sug7=100&bs=%E5%9C%B0%E5%9D%80%E6%A0%8F%E7%BC%96%E7%A0%81%E4%B8%8E%E6%B1%89%E5%AD%97%20js%E4%BA%92%E8%BD%AC" ) var wd = decodeURIComponent(baidu.wd) var oq = decodeURIComponent(decodeURI(baidu.oq)) var bs = decodeURIComponent(baidu.bs) var rsv_t = decodeURIComponent(baidu.rsv_t) var rsv_pq = baidu.rsv_pq console.log( 'baidu...' ,baidu); console.log( 'wd...' ,wd); console.log( 'oq...' ,oq); console.log( 'bs...' ,bs); console.log( 'rsv_t...' ,rsv_t); console.log( 'rsv_pq...' ,rsv_pq); 5、用正则封装解析方法(HASH值) function hashA(url){ var reg=/#([^#&=?]+)/; if (reg.test(url)){ return reg.exec(url)[1]; } } console.log(hashA( "https://www.baidu.com/newspage/data/landingsuper?AAA=1111&BBB=222&CCC=333#1234" )); 6、用正则封装解析方法 var ary = [ "零" , "一" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九" ]; var str = "今年是2017年" ; var result = str.replace(/\d/g,function (value) { return ary[value] }); console.log( result ); 7、用正则择出汉字 var str = '<img src="haha.png" alt="你的朋友"/>' ; var reg=/alt= "(.+)" /; console.log(reg.exec(str)[1]); 8、someNumber为数字,当它的小数位数少于number时,则保留原数字,否则四舍五入保留至小数number位 function formatTime(someNumber, number) { var lastNum = null ; if ( typeof someNumber == "number" ) { var strNum = someNumber.toString().match(/\.\d*/); if (strNum == null || strNum[0].length <= number + 1) { lastNum = someNumber; } else { lastNum = someNumber.toFixed(number); } } return lastNum; } console.log(formatTime(3.1283456, 2)); console.log(formatTime(3.55, 3)); console.log(formatTime(3, 3)); 五、数组方法(数组的方法) 菜鸟教程,https: //www.runoob.com/js/js-obj-array.html MDN WEB,https: //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array 注意,数组也有的方法,如concat、includes、indexOf、slice 附、区别 (1)every、some,返回 true 或 false (2)filter、find,返回多项或单项 1、concat (1)作用:把多个数组拼接在一起 (2)参数:要拼接的数组或字符串。 如果是数组,就把数组里的每一项放到原数组的后面; 如果是字符串,把字符串作为一项,放在原数组后面; 浅克隆数组:ary.concat();ary.concat([]); var obj = {b:3} var ary = [1,2,obj] var aaa = ary.concat(); console.log( obj == aaa[2] ); //true (3)返回值:拼接好的数组 (4)是否改变原数组:不变 2、every (1)方法用于检测数组的所有元素是否-都-满足指定条件,返回值为boolean const array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every((curr) => curr < 40)); //true 3、filter (1)作用:找出所有满足条件的项 (2)参数:匿名函数,匿名函数可以有三个参数 (3)返回值:所有满足条件的项组成的数组 const array = [10, 20, 30, 40, 50]; const single = array.filter(function(num){ return num > 30 }); console.log(single, typeof single); //[ 40, 50 ] object (4)是否改变原数组:不变 (5)示例一 var aryOld = [1, 2, 3, 4, 5]; var aryNew=aryOld.filter(function(item){ return item > 3; }); console.log(aryNew); (6)示例二 var todos = [{ score: 10, completed: true }, { score: 2, completed: false }, { score: 30, completed: true }, { score: 40, completed: true }, { score: 5, compelted: false }]; var trueScore = todos.filter(function(item){ return item.completed; }); console.log(trueScore); 4、find (1)作用:找出最先满足条件的那一项 (2)参数:匿名函数,匿名函数可以有三个参数 (3)返回值:最先满足条件的那一项 const array = [10, 20, 30, 40, 50]; const single = array.find(function(num){ return num > 30 }); console.log(single, typeof single); //40 number (4)是否改变原数组:不变 (5)示例一 var ary=[1, 5, 10, 15] ary.find(function(value) { return value > 9; }) // 10 (6)示例二 var studentAll = [ { name: '张三' , gender: '男' , age: 20 }, { name: '王小毛' , gender: '男' , age: 20 }, { name: '李四' , gender: '男' , age: 20 } ]; var studentOne=studentAll.find(function (ele) { return ele.age == 20 }); console.log(studentOne); 5、forEach (1)作用:循环遍历每一项,循环次数为数组项数 (2)参数:匿名函数,匿名函数可以有三个参数,项、项索引、数组 (3)返回值:undefined (4)是否改变原数组:不变 6、includes (1)作用:判断数组中是否有某项 (2)参数:要判断的数组项 (3)返回值:boolean (4)是否改变原数组:不变 var pets = [ 'cat' , 'dog' , 'bat' ]; console.log(pets.includes( 'cat' )); //true 7、indexOf (1)作用:获取数组项第一次在数组中出现的位置索引 (2)参数:要判断的数组项 (3)返回值:数组项的索引,没有该项返回-1 (4)是否改变原数组:不变 8、 join (与字符串里split方法相反) (1)作用:把数组的每一项按照指定的分隔符拼接成字符串 (2)参数:指定的分隔符,项与项之间用分隔符连接 (3)返回值:拼接好的字符串 (4)是否改变原数组:不变 (5)实例1:console.log([ "ABC" , "EFG" ]. join ( "" )); //ABCEFG (6)实例2:console.log([ "ABC" , "EFG" ]. join ( "D" )); //ABCDEFG 9、map (1)作用:循环遍历每一项,循环次数为数组项数 (2)参数:匿名函数,匿名函数可以有三个参数,项、项索引、数组 (3)返回值:返回一个新数组,新数组的每一项是匿名函数每次执行后的返回值 (4)是否改变原数组:不变 10、pop (1)作用:删除项数组末尾项 (2)参数:没有参数 (3)返回值:被删除的项 (4)是否改变原数组:改变 11、push (1)作用:向数组末尾追加一项 (2)参数:要添加的数组项(多项用逗号隔开) (3)返回值:新数组的长度 (4)是否改变原数组:改变 12、reduce (1)累加,将最后一次计算结果暴露出去;可以作为高阶函数,用于函数的compose (2)接受四个参数:初始值(或回调函数上1次的返回值),当前元素值,当前索引,调用 reduce 的数组 (3)示例一 var ary=[1,3,2,4,3,2]; var aryTotal=ary.reduce(function (total,num) { return total+num; }); console.log(aryTotal); (4)示例二 var todos = [{ score: 12, completed: true }, { score: 21, completed: false }, { score: 35, completed: true }, { score: 47, completed: true }, { score: 95, completed: false }]; const totalScore = todos.reduce(function(total, item){ return item.completed ? total + item.score : total },10000); console.log(totalScore); 13、reverse (1)作用:倒序数组 (2)参数:不需要参数 (3)返回值:倒序后的数组 (4)是否改变原数组:改变 示例:console.log([ '21' , 'a' , 'e' , 'c' ].reverse()); 14、shift (1)作用:删除数组开头项 (2)参数:没有参数 (3)返回值:被删除的项 (4)是否改变原数组:改变 15、slice (1)作用:复制数组项 (2)没有参数:原数组不变,返回整个数组 (3)一个参数:返回从索引位置到末尾 (4)两个参数(n,m):返回索引n到m,不包含m (5)是否改变原数组:不变 另,console.log( [7,6,5,4,3,2,1].slice(-3) ); //从后往前取3项,构成的新数组 16、some (1)方法用于检测数组的所有元素是否-有-满足指定条件,返回值为boolean const array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.some((curr) => curr < 10)); //true 17、sort (1)作用:按照指定规则排序 (2)参数: A、无 var ary = [ '21' , 'a' , 32, '4' , 5, '61' , 'e' , 'f' , 'd' , 'b' , 'c' ]; ary.sort(); //升序,先比较每项的第1个字符,相同则比较第2个字符 console.log(ary) B、回调函数-纯数字项数组 var ary = [21, 32, 32, 5, 61, 61, 21, 4, 5, 61]; ary.sort(function (a, b) { return a - b //升序,比较数字的大小 }); console.log(ary) C、回调函数-任意项数组 var ary = [ { id: 2, name: 'b' , age: 18 }, { id: 3, name: 'c' , age: 20 }, { id: 1, name: 'a' , age: 19 } ]; ary.sort(function (a,b) { //return a.id - b.id return a.name.localeCompare(b.name) ; //返回值为-1,0,1,表示(前者在后者)之前、相等、之后 }); console.log(ary) (3)返回值:排好序的数组 (4)是否改变原数组:改变 18、splice (1)作用:参数不同,作用不同,主要用来删除数组项 (2)没有参数:原数组不变,返回空数组 (3)一个参数:从索引位置删除到末尾,返回被删除项构成的数组 (4)两个参数(n,m):从索引n删除m个,返回被删除项构成的数组 (5)三个参数(n,m,X): 从索引n删除m个,被删除项用X填补,返回被删除项构成的数组 (6)是否改变原数组:改变 /* ===以上原数组改变;以下原数组不变=== */ 19、unshift (1)作用:向数组开头添加一项 (2)参数:要添加的数组项(多项用逗号隔开) (3)返回值:新数组的长度 (4)是否改变原数组:改变 20、call和apply (1)call的第2、3、4个参数,分别对应函数的第1、2、3个参数 (2)apply的第2个参数是一个数组,数组的第1、2、3项,分别对应函数的第1、2、3个参数 示例一 function test(a,b) { console.log( this ) //默认情况下函数中的this指向的是Window对象 console.log(a,b) //a,b对应的是参数1和参数2 } const obj= { name: 'zhangsan' } test.call(obj, '参数1' , '参数2' ) test.apply(obj,[ '参数1' , '参数2' ]) //数组的第一、二项,分别对应参数的第一、二个 示例二 var array1 = [1,1,1,1,1]; var array2 = [2,2,2,2,2]; array1.push.call(array2,6,7,8,9); //改变了array2 array2.push.apply(array1,[9,8,7,6]); //改变了array1 console.log(array1) console.log(array2) 21、数组4种去重方法 (1)新数组法:重新构建一个新数组,遍历原数组的每一项,如果该项在新数组的索引为-1,则把该项添加到新数组中。 var ary = [21, 32, 32, 4, 5, 61, 61, 21, 4, 5, 61]; function removeMany(ary){ var newAry=[]; for ( var i=0; i<ary.length; i++){ if (newAry.indexOf(ary[i])===-1){ newAry[newAry.length]=ary[i]; } } return newAry } console.log(removeMany(ary)) (2)新对象法:如果对象中没有这一项,给对象添加属性名和属性值,如果对象中已经有了,说明是重复的,直接在数组中删除这一项; var ary = [21, 32, 32, 4, 5, 61, 61, 21, 4, 5, 61]; function removeMany(ary){ var obj={}; for ( var i=0; i<ary.length; i++){ var cur=ary[i]; if (obj[cur]===cur){ //说明重复了 ary.splice(i,1); i--; } else { obj[cur]=cur; } } return ary } console.log(removeMany(ary)) (3)sort排序法:先用sort方法对数组进行排序,然后拿前一项与后一项进行比较,如果相等,则删除前一项,同时为了防止数组塌陷,下一次仍从该项开始比较。 var ary = [21, 32, 32, 4, 5, 61, 61, 21, 4, 5, 61]; function removeMany(ary){ ary.sort(function (a, b) { return a - b; //升序 }); for ( var i = 0; i < ary.length; i++) { if (ary[i] === ary[i + 1]) { ary.splice(i, 1); i--; } } return ary } console.log(removeMany(ary)) (4)双循环法:第一个循环,依次取出数组的每一项A;第二个循环,从数组的A项开始,依次与A项进行比较,如果相等则删除,同时为了防止数组塌陷,下一次仍从该项开始比较。 var ary = [21, 32, 32, 4, 5, 61, 61, 21, 4, 5, 61]; function removeMany(ary){ for ( var i = 0; i < ary.length; i++) { var cur = ary[i]; for ( var j = i + 1; j < ary.length; j++) { if (cur === ary[j]) { ary.splice(j, 1); j--; } } } return ary } console.log(removeMany(ary)) 22、数组3种排序方法 (1)冒泡排序(目标:实现升序排列) var ary = [5, 4, 3, 2, 1]; function bubbleSort(ary) { for ( var i = 0; i < ary.length - 1; i++) { //正在进行第几次循环 for ( var j = 0; j < ary.length - i - 1; j++) { //本次循环需要执行几次 if (ary[j] > ary[j + 1]) { var tmp = ary[j]; ary[j] = ary[j + 1]; ary[j + 1] = tmp; } } console.log(ary); } return ary; } console.log(bubbleSort(ary)) (2)插入排序(目标:实现升序排列) var ary = [50, 40, 30, 20, 10]; function insertSort(ary) { var left = ary.splice(0, 1); for ( var i = 0; i < ary.length; i++) { //正在进行第几次循环 var aryAry = ary[i]; for ( var j = left.length - 1; j >= 0; ) { //本次循环需要执行几次 var leftAry = left[j]; if (aryAry < leftAry) { j--; if (j === -1) { left.unshift(aryAry); } } else { left.splice(j + 1, 0, aryAry); break ; } } } return left; } console.log(insertSort(ary)); (3)快速排序(目标:实现升序排列) var ary = [500, 400, 300, 200, 100]; function quickSort(ary) { if (ary.length <= 1) { return ary; } var num = Math.floor(ary.length / 2); var numValue = ary.splice(num, 1)[0]; var left = []; var right = []; for ( var i = 0; i < ary.length; i++) { var cur = ary[i]; if (cur < numValue) { left.push(cur); } else { right.push(cur); } } return quickSort(left).concat([numValue], quickSort(right)); } console.log(quickSort(ary)); 六、对象方法(对象的方法) 附、js对象之属性获取说明 A、只能获取自身属性:Object.keys(仅含可枚举属性)、Object.getOwnPropertyNames(还含不可枚举属性)、Object.hasOwnProperty(还含不可枚举属性) B、获取自身属性、原型链属性:打点、 for ... in 、 in 附、js对象之可枚举属性说明 A、可以通过某些方法(如 for ... in 循环、Object.keys())被遍历或访问的属性 B、由描述符(enumerable)决定的,默认为 true C、自身属性、原型链属性有的可枚举,有的不可枚举 菜鸟教程,https: //www.runoob.com/jsref/dom-obj-object.html MDN WEB,https: //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object 1、Object.keys(obj),返回一个给定对象自身可枚举属性的名称数组 const obj = { a: 1, b: 2 }; const keys = Object.keys(obj); console.log(keys); // [ 'a', 'b' ] 2、Object.values(obj),返回一个给定对象自身可枚举属性值的数组 const obj = { a: 1, b: 2 }; const values = Object.values(obj); console.log(values); // [ 1, 2 ] 3、Object.entries(obj),返回一个给定对象自身可枚举属性的键值对数组 const obj = { a: 1, b: 2 }; const entries = Object.entries(obj); console.log(entries); // [ [ 'a', 1 ], [ 'b', 2 ] ] 4、Object.assign(target,...sources),用于将所有可枚举属性的值从一个或多个源对象复制到目标对象,并返回目标对象 const target = { a: 1, b: 2 }; const source = { b: 3, c: 4 }; const result = Object.assign(target, source); console.log(result); // { a: 1, b: 3, c: 4 } 5、Object.create(proto, [propertiesObject]),创建一个新对象,使用指定的原型对象和可选的属性描述符对象 const personProto = { sayHello() { console.log( 'Hello!' ); }, }; const person = Object.create(personProto); person.sayHello(); // Hello! 6、Object.defineProperty(obj, prop, descriptor),用于定义对象的新属性或修改现有属性,并返回修改后的对象 const obj = {}; Object.defineProperty(obj, 'property1' , { value: 42, writable: false , }); console.log(obj.property1); // 42 obj.property1 = 43; // 由于 writable 为 false,赋值操作无效 console.log(obj.property1); // 42 7、Object.freeze(obj),冻结一个对象,使其不可修改、添加或删除属性 const obj = { a: 1 }; Object.freeze(obj); obj.a = 2; // 操作被忽略,因为对象已被冻结 console.log(obj.a); // 1 8、Object. is (obj1, obj2),确定两个值是否是相同的值,与严格相等运算符(===)类似 console.log(Object. is (2, 2)); // true console.log(Object. is (NaN, NaN)); // true console.log(Object. is (0, -0)); // false console.log(Object. is ({a:1}, {a:1})); // false 七、递归 注意,递归前从外向里执行。递归后从里向外执行。 1、虚拟DOM中的递归 //虚拟DOM,Virtual DOM,堆内存的一个JS对象,包含很多虚拟节点VNode,Virtual Node <!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title>创建一个虚拟DOM实例</title> <style> .list { color: green; font-size: 30px; } </style> </head> <body> <div> <div>创建一个虚拟DOM实例</div> </div> <script> function Element(obj) { this .tagName = obj.tagName; this .props = obj.props || {}; this .children = obj.children || []; } Element.prototype.render = function () { var el = document.createElement( this .tagName); var props = this .props; for (propName in props) { propValue = props[propName]; el.setAttribute(propName, propValue); } this .children.forEach(function (child) { var elemChild = null ; if (child instanceof Element) { elemChild = child.render(); //从外向里逐个折回。看最后一次递归执行时,发生了什么。 } else { elemChild = document.createTextNode(child); //最后一次递归执行时,要经过这里。 } el.appendChild(elemChild); //从里向外逐个添加。 }); return el; }; var elem = new Element({ tagName: 'ul' , props: { 'class' : 'list' }, children: [ new Element({ tagName: 'li' , children: [ new Element({ tagName: 'span' , children: [ '我是span1' , '我是span1' , '我是span1--' ] }), new Element({ tagName: 'span' , children: [ '我是span2' , '我是span2' , '我是span2' ] }) ] }), new Element({ tagName: 'li' , children: [ '我是span3' , '我是span3' , '我是span3' ,] }) ] }); var elemSelf = elem.render(); document.querySelector( 'body' ).appendChild(elemSelf); </script> </body> </html> 2、快速排序中的递归 var ary = [500, 400, 300, 200, 100]; function quickSort(ary) { if (ary.length <= 1) { return ary; } var num = Math.floor(ary.length / 2); var numValue = ary.splice(num, 1)[0]; var left = []; var right = []; for ( var i = 0; i < ary.length; i++) { var cur = ary[i]; if (cur < numValue) { left.push(cur); } else { right.push(cur); } } return quickSort(left).concat([numValue], quickSort(right)); } console.log(quickSort(ary)); 3、react框架中的递归 (function (allFn) { if ( typeof exports === "object" && typeof module !== "undefined" ) { module.exports = allFn() } else if ( typeof define === "function" && define.amd) { define([], allFn) } else { var tempGlobal; if ( typeof window !== "undefined" ) { tempGlobal = window } else if ( typeof global !== "undefined" ) { tempGlobal = global } else if ( typeof self !== "undefined" ) { tempGlobal = self } else { tempGlobal = this } tempGlobal.React = allFn() } })(function () { var define, module, exports; return (function outerFn(first, second, third) { function recursion(number) { if (!second[number]) { if (!first[number]) { var error = new Error( "Cannot find module '" + number + "'" ); throw error.code = "MODULE_NOT_FOUND" , error } var module = second[number] = { exports: {} }; first[number][0].call(module.exports, function (key) { var value = first[number][1][key]; return recursion(value ? value : key) }, module, module.exports, outerFn, first, second, third) } return second[number].exports //在react实例化的过程中,这行代码不但因获取依赖而多次执行,而且还因获取react实例而最后执行。 } for ( var number = 0; number < third.length; number++) recursion(third[number]); //fn(16)第1次执行,执行结果没有变量接收 return recursion //执行到这,整个逻辑就快结束了。前两行可以合并为一行:return recursion(third[0]),同时下面的"(48)"应当删掉。 })( { 2: [function (_dereq_, module, exports) { var thisVar = _dereq_(138) }, { "25" : 25, "30" : 30 }], }, { }, [16] )(16) // fn(16)第2次执行,因为n[num]为真,所以直接返回n[num].exports并被挂在g.React上 }); 八、两行布局 1、固定高,此增彼减;用flex计算下高 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>Title</title> <style> * { margin: 0; padding: 0; } .full { height: 600px; background: red; display: flex; /* 相关代码 */ flex-direction: column; /* 相关代码 */ } .up { background: blue; } .down { background: green; flex: 1; /* 相关代码 */ /*height:100%;*/ } </style> </head> <body> <div class = "full" > <div class = "up" > <p>ABCD</p> </div> <div class = "down" > <p>DCBA</p> </div> </div> </body> </html> 1、正好占满全屏 (1)用calc计算下高、下右宽 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>Title</title> <style> * { padding: 0; margin: 0; } html, body { height: 100%; } .up { height: 50px; background: #BBE8F2; width: 100%; } .down { height: calc(100% - 60px); /* 相关代码 */ background: #D9C666; margin-top: 10px; font-size: 0; display: flex; /* 相关代码 */ } .left { width: 200px; height: 100%; overflow: auto; background: palevioletred; margin-right: 10px; font-size: 16px; } .right { width: calc(100% - 210px); /* 相关代码 */ height: 100%; overflow: auto; background: rebeccapurple; font-size: 16px; } </style> </head> <body> <div class = "up" ></div> <div class = "down" > <div class = "left" > </div> <div class = "right" > </div> </div> </body> </html> (2)用flex计算下高、下右宽 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>Title</title> <style> * { padding: 0; margin: 0; } html, body { height: 100%; } .body { display: flex; flex-direction: column; } .up { height: 50px; background: #BBE8F2; width: 100%; } .down { flex: 1; background: #D9C666; font-size: 0; display: flex; } .left { width: 200px; height: 100%; overflow: auto; background: rebeccapurple; font-size: 16px; margin-right: 10px; } .right { flex: 1; overflow: auto; background: rebeccapurple; font-size: 16px; } </style> </head> <body class = "body" > <div class = "up" ></div> <div class = "down" > <div class = "left" ></div> <div class = "right" ></div> </div> </body> </html> 九、两列布局 附、两个需求 (1)左侧固定宽、右侧占满余屏 (2)左、右自适应高 附、四个实现 (1)table (2)flex (3)内外补+浮动 (4)大高赋给小高 1、table <!DOCTYPE html> <html lang= "en" > <head> <title>两列高度自适应</title> <style type= "text/css" > * { margin: 0; padding: 0; } </style> </head> <body> <table style= "width:100%" > <tr> <td style= "width:200px;background: #ddd;" > <br/> </td> <td style= "width:calc(100% - 200px);background: #ddd;" > <br/> <br/> <br/> <br/> </td> </tr> </table> <p>我是尾部,我是尾部,我是尾部,我是尾部,我是尾部,我是尾部!</p> </body> </html> 2、flex <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title></title> <style> * { margin: 0; padding: 0; } </style> </head> <body> <div style= "display:flex" > <div style= "width:200px;background: #ddd;margin-right: 10px;" ><br/></div> <div style= "flex:1;background: #ddd;" ><br/><br/><br/><br/><br/></div> </div> </body> </html> 3、内外补+浮动 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>两列高度自适应</title> <style type= "text/css" > * { margin: 0; padding: 0; } #main { overflow: hidden; } .both { background: #ddd; padding-bottom: 10000px; margin-bottom: -10000px; } .left { float : left; width: 200px; } .right { margin-left: 210px; } </style> </head> <body> <div id= "main" > <div class = "left both" ><br/></div> <div class = "right both" ><br/><br/><br/><br/><br/></div> </div> <p>我是尾部,我是尾部,我是尾部,我是尾部,我是尾部,我是尾部!</p> </body> </html> 4、大高赋给小高 (1)浮动 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title></title> <style> * { margin: 0; padding: 0; } #left { float : left; width: 200px; background: #ddd; } #right { margin-left: 210px; background: #ddd; } </style> </head> <body> <div> <div id= "left" ><br/><br/><br/></div> <div id= "right" ><br/><br/><br/><br/><br/><br/><br/></div> </div> <p>我是尾部,我是尾部,我是尾部,我是尾部,我是尾部,我是尾部!</p> </body> </html> <script type= "text/javascript" > var left = document.getElementById( "left" ); var right = document.getElementById( "right" ); var leftHeight = parseFloat(getComputedStyle(left).height); var rightHeight = parseFloat(getComputedStyle(right).height); var distanceHeight = leftHeight - rightHeight; if (distanceHeight > 0) { right.style.height = leftHeight + "px" ; //right.style.height =rightHeight +distanceHeight+ "px"; //right.style.height只能赋值,不能获取,所以此处用rightHeight代替right.style.height } else { left.style.height = rightHeight + "px" ; //left.style.height = leftHeight-distanceHeight + "px"; } </script> (2)定位 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>左右固定宽,中间自适应</title> <style type= "text/css" > * { margin: 0; padding: 0; } .left { top: 0; left: 0; position: absolute; width: 200px; background: #ddd; } .right { top: 0; left: 210px; right: 0; position: absolute; background: #ddd; } </style> </head> <body> <div id= "left" class = "left" ><br/><br/><br/><br/><br/><br/></div> <div id= "right" class = "right" ><br/></div> </body> </html> <script type= "text/javascript" > var left = document.getElementById( "left" ); var right = document.getElementById( "right" ); var leftHeight = parseFloat(getComputedStyle(left).height); var rightHeight = parseFloat(getComputedStyle(right).height); var distanceHeight = leftHeight - rightHeight; if (distanceHeight > 0) { right.style.height = leftHeight + "px" ; //right.style.height =rightHeight +distanceHeight+ "px"; //right.style.height只能赋值,不能获取,所以此处用rightHeight代替right.style.height } else { left.style.height = rightHeight + "px" ; //left.style.height = leftHeight-distanceHeight + "px"; } </script> 5、jQuery实现多div等高(与方案4相似) $(function() { var maxHeight = 0; //以下求最高的div的高度 $( ".height" ).each(function() { var thisHeight = $( this ).innerHeight(); maxHeight = thisHeight > maxHeight ? thisHeight : maxHeight; }) //以下将最高的div的高度赋值给每一个(需要与最高div等高的)div, $( ".height" ).each(function() { var thisPadding = $( this ).innerHeight() - $( this ).height(); //在jQuery中,innerheight=padding+内容height $( this ).height(maxHeight - thisPadding); }) }) 十、三列布局 附、两个需求 (1)左右两侧固定宽、中间占满余屏 (2)左、中、右自适应高 附、四个实现 (1)table (2)flex (3)内外补+浮动 (4)大高赋给小高 1、table <!DOCTYPE html> <html lang= "en" > <head> <title>两列高度自适应</title> <style type= "text/css" > * { margin: 0; padding: 0; } </style> </head> <body> <table style= "width:100%" > <tr> <td style= "width:200px;background: #ddd;" ><br/></td> <td style= "width:calc(100% - 400px);background: #ddd;" ><br/><br/><br/><br/></td> <td style= "width:200px;background: #ddd;" ><br/></td> </tr> </table> <p>我是尾部,我是尾部,我是尾部,我是尾部,我是尾部,我是尾部!</p> </body> </html> 2、flex <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>左右固定宽,中间自适应</title> <style> * { margin: 0; padding: 0; } .flex { display: flex; } .leftAndRight{ width: 200px; background: #ddd; } .middle{ flex: 1; margin:0 10px; background: #ddd; } </style> </head> <body> <div class = "flex" > <div class = "leftAndRight" ><br/></div> <div class = "middle" ><br/><br/><br/><br/></div> <div class = "leftAndRight" ><br/></div> </div> </body> </html> 3、内外补+浮动 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>三列高度自适应</title> <style type= "text/css" > * { margin: 0; padding: 0; } #main { overflow: hidden; } .three{ background: #ddd; padding-bottom: 10000px; margin-bottom: -10000px; float : left; } .leftAndRight { width: 200px; } .middle { margin-left: 10px; margin-right: 10px; width:calc(100% - 420px) } </style> </head> <body> <div id= "main" > <div class = "leftAndRight three" ><br/></div> <div class = "middle three" ><br/><br/><br/><br/><br/></div> <div class = "leftAndRight three" ><br/></div> </div> <p>我是尾部,我是尾部,我是尾部,我是尾部,我是尾部,我是尾部!</p> </body> </html> 4、大高赋给小高 (1)浮动 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>三列高度自适应</title> <style type= "text/css" > * { margin: 0; padding: 0; } .three{ background: #ddd; float : left; } .leftAndRight { width: 200px; } .middle { margin-left: 10px; margin-right: 10px; width:calc(100% - 420px) } </style> </head> <body> <div> <div class = "leftAndRight three" id= "left" ><br/></div> <div class = "middle three" id= "middle" ><br/><br/><br/><br/><br/></div> <div class = "leftAndRight three" id= "right" ><br/></div> </div> <p>我是尾部,我是尾部,我是尾部,我是尾部,我是尾部,我是尾部!</p> </body> </html> <script type= "text/javascript" > var left = document.getElementById( "left" ); var middle = document.getElementById( "middle" ); var right = document.getElementById( "right" ); var leftHeight=parseFloat(getComputedStyle(left).height); var middleHeight=parseFloat(getComputedStyle(middle).height); var rightHeight=parseFloat(getComputedStyle(right).height); var maxHeight=0; if (leftHeight-middleHeight>0){ maxHeight= leftHeight; } else { maxHeight= middleHeight; } if (rightHeight-maxHeight>0){ maxHeight= rightHeight; } left.style.height =maxHeight+ "px" ; middle.style.height =maxHeight+ "px" ; right.style.height =maxHeight+ "px" ; </script> (2)定位 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>左右固定宽,中间自适应</title> <style type= "text/css" > * { margin: 0; padding: 0; } .leftAndRight { top: 0; width: 200px; position: absolute; background: #ddd; } .left { left: 0; } .right { right: 0; } .middle { top: 0; left: 210px; right: 210px; background: #ddd; position: absolute; } </style> </head> <body> <div id= "left" class = "left leftAndRight" ><br/><br/></div> <div id= "middle" class = "middle" ><br/><br/><br/><br/><br/><br/></div> <div id= "right" class = "right leftAndRight" ><br/></div> </body> </html> <script type= "text/javascript" > var left = document.getElementById( "left" ); var middle = document.getElementById( "middle" ); var right = document.getElementById( "right" ); var leftHeight=parseFloat(getComputedStyle(left).height); var middleHeight=parseFloat(getComputedStyle(middle).height); var rightHeight=parseFloat(getComputedStyle(right).height); var maxHeight=0; if (leftHeight-middleHeight>0){ maxHeight= leftHeight; } else { maxHeight= middleHeight; } if (rightHeight-maxHeight>0){ maxHeight= rightHeight; } left.style.height =maxHeight+ "px" ; middle.style.height =maxHeight+ "px" ; right.style.height =maxHeight+ "px" ; console.log(maxHeight) </script> 十一、五种盒子四方居中 1、4种定位四方居中 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>4种定位四方居中</title> <style> #mask1_1 { width: 100%; height: 100%; position: fixed ; left: 0; top: 0; display: flex; justify-content: center; align-items: center; } #content1_1{ background-color: gray; } #mask2_4 { width: 100%; height: 100%; position: fixed ; left: 0; top: 0; } #content2_2{ position: fixed ; left: 50%; /* 父级宽度的50% */ top: 50%; transform: translate(-50%, -50%); /* 自身宽度的50% */ background-color: gray; } #content3_3{ position: fixed ; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 400px; height: 300px; background-color: gray; } #content4_4{ position: fixed ; background-color: gray; } </style> </head> <body> <div id= "mask1_1" ><div id= "content1_1" >居中一</div></div> <!-- <div id= "mask1_1" ><div id= "content1_1" >居中一</div></div> --> <!-- <div id= "mask2_4" ><div id= "content2_2" >居中二</div></div> --> <!-- <div id= "mask2_4" ><div id= "content3_3" >居中三</div></div> --> <!-- <div id= "mask2_4" ><div id= "content4_4" >居中四</div></div> --> </body> </html> <script> var oDiv = document.getElementById( 'content4_4' ); if (oDiv){ var clientWidth = document.documentElement.clientWidth || document.body.clientWidth; var clientHeight = document.documentElement.clientHeight || document.body.clientHeight; oDiv.style.left = (clientWidth - oDiv.clientWidth) / 2 + 'px' ; oDiv.style.top = (clientHeight - oDiv.clientHeight) / 2 + 'px' ; } </script> 2、1种无定位四方居中 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>CSS无定位居中</title> <style> .outer{ width: 1000px; height: 500px; background: gray; text-align: center; /* 此处,左右居中 */ } /* 以下,上下居中 */ .middleShow{ width: 200px; height: 100px; display: inline-block; vertical-align: middle; /* 以下,标识区域 */ background: gainsboro; } .middleHide{ width: 0; height: 100%; display: inline-block; vertical-align: middle; } </style> </head> <body> <div class = "outer" > <div class = "middleShow" ></div> <div class = "middleHide" ></div> </div> </body> </html> 3、三种横向换行也居中 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>3种横向换行也居中</title> <style> .center{ display: flex; flex-wrap: wrap; flex-direction: column; border: 1px solid green; align-items: center; /* 独特之处 */ } .center1{ display: flex; flex-wrap: wrap; flex-direction: row; border: 1px solid green; justify-content: center; /* 独特之处 */ } .center2{ display: flex; flex-wrap: wrap; flex-direction: row; border: 1px solid green; justify-content: space-around; /* 独特之处 */ } .center3{ display: flex; flex-wrap: wrap; flex-direction: row; border: 1px solid green; justify-content: space-between; /* 独特之处 */ } .center > div,.center1 > div,.center2 > div,.center3 > div{ width: 400px; height: 80px; border: 1px solid red; } </style> </head> <body> <div class = "center" > <div>竖向换行也居中</div> <div></div> </div> <div class = "center1" > <div>横向换行也居中-1,之间无间距</div> <div>justify-content: center</div> </div> <div class = "center2" > <div>横向换行也居中-2,之间大间距</div> <div>justify-content: space-around</div> </div> <div class = "center3" > <div>横向换行也居中-3,之间超大间距</div> <div>justify-content: space-between</div> </div> </body> </html> 十二、em、rem自适应 1、em示例 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>em示例</title> </head> <body> <div style= "font-size: 50px" > <div style= "width: 20em;height:4em;background: red;border:1px solid green;border-radius:10px;font-family:'楷体';" > 用em做单位的都是相对于父级字体大小的,本例父级字体的大小是50px,所以这里的宽20em就是1000px,高4em就是200px! </div> </div> </body> </html> 2、rem自适应示例 (1)rem自适应示例之css版(实用程度低) <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>rem自适应示例之css版(实用程度低)</title> <style> html { font-size: 20px; } /* :root { font-size: 20px; } */ </style> </head> <body> <div style= "font-size: 1.6rem" > <div style= "width: 50rem;height:18rem;background: red;border:1px solid green;border-radius:10px;font-family:'楷体';padding-left:20px;" > <p>根字体的大小即1rem,用rem做单位的都是相对于根字体大小的。</p> <p>CSS设置根字体,有2种方法,见style标签</p> </div> </div> </body> </html> (2)rem自适应示例之js版(实用程度高) <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>rem自适应示例之js版(实用程度高)</title> </head> <body> <div style= "font-size: 16rem" > <div style= "width: 500rem;height:180rem;background: red;border:1px solid green;border-radius:10px;font-family:'楷体';padding-left:20px;" > <p>根字体的大小即1rem,用rem做单位的都是相对于根字体大小的。</p> <p>下面把1rem定义为:(屏幕宽度/设计稿的宽度)+ "px" 。</p> <p>document.documentElement.style.fontSize=</p> <p>document.documentElement.clientWidth/750+ "px" ;</p> </div> </div> </body> </html> <script> var designWidth = 1000 ; document.documentElement.style.fontSize = document.documentElement.clientWidth/designWidth+ "px" ; </script> 十三、Flex布局 1、flex是2009年,W3C提出了一种布局方案,子元素的 float 、clear和vertical-align属性失效 (1)一个固定,另一个占据剩余宽或高 .box{ width: 100%; //height: 100% display: flex; flex-direction: row; //flex-direction: column; } .box>.div1{ width:100px; //height: 100px; } .box>.div2{ flex:1; } height: calc(100vh - 200px) (2)横向按钮组居中居右 .div{ display: flex; justify-content: center; //flex-end } .div button{ margin-left: 20px; } (3)行内元素也可以使用 Flex 布局。 .box{ display: inline-flex; } 2、flex作为属性,区别于上面作为属性值 来源,https: //blog.csdn.net/Shivy_/article/details/122361132 来源,https: //www.runoob.com/w3cnote/flex-grammar.html .item { flex: flex-grow flex-shrink flex-basis flex: 1 1 //可放大,可缩小 flex: 0 0 //不放大,不缩小 } (1)flex-grow,项目的放大比例,默认为0,即使存在剩余空间,也不放大;设为1,子盒子占满父盒子的剩余空间 (2)flex-shrink,属性定义了项目的缩小比例,设为0,即使空间不足,也不缩小;默认为1,如果空间不足,该项目将缩小 (3)flex-basis,属性定义了在分配多余空间之前,项目占据的主轴空间,默认值为auto,即项目的本来大小 3、flex-direction:主轴的方向 .box { flex-direction: row | row-reverse | column | column-reverse; } (1)row(默认值):主轴为水平方向,起点在左端 (2)row-reverse:主轴为水平方向,起点在右端 (3)column:主轴为垂直方向,起点在上沿 (4)column-reverse:主轴为垂直方向,起点在下沿 4、flex-wrap:主轴的换行 .box{ flex-wrap: nowrap | wrap | wrap-reverse; } (1)nowrap(默认):不换行 (2)wrap:换行,第一行在上方 (3)wrap-reverse:换行,第一行在下方 5、justify-content:主轴的对齐 .box { justify-content: flex-start | flex-end | center | space-around | space-between; } 具体对齐方式与轴的方向有关 (1)flex-start(默认值):主轴起点对齐 (2)flex-end:主轴终点对齐 (3)center: 主轴居中对齐,之间0间距 (4)space-around:主轴居中对齐,之间小间距,项目与外边框的间隔,是项目之间间隔的一半 (5)space-between:主轴两端对齐,之间大间距,项目与外边框的间隔,是0 6、align-items:交叉轴对齐 .box { align-items: flex-start | flex-end | center | baseline | stretch; } 具体的对齐方式与交叉轴的方向有关 (1)flex-start:交叉轴的起点对齐 (2)flex-end:交叉轴的终点对齐 (3)center:交叉轴的中点对齐 (4)baseline: 项目的第一行文字的基线对齐 (5)stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度 7、flex-flow:flex-direction和flex-wrap的简写,默认值为row nowrap .box { flex-flow: <flex-direction> || <flex-wrap>; } 8、align-content属性 align-content属性定义了多根轴线的对齐方式如果项目只有一根轴线,该属性不起作用 .box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; } (1)flex-start:与交叉轴的起点对齐 (2)flex-end:与交叉轴的终点对齐 (3)center:与交叉轴的中点对齐 (4)space-between:与交叉轴两端对齐,轴线之间的间隔平均分布 (5)space-around:每根轴线两侧的间隔都相等所以,轴线之间的间隔比轴线与边框的间隔大一倍 (6)stretch(默认值):轴线占满整个交叉轴 十四、多图片延迟加载下载 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title></title> <style> * { margin: 0; padding: 0; } img { border: none; } div { margin: 0 auto; width: 800px; height: 400px; background: url( "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518060703_mthumb.jpg" ) no-repeat center #e1e1e1; } div img { width: 100%; height: 100%; } p { width: 800px; height: 600px; line-height: 60px; background: lightgrey; font-size: 30px; margin: 0 auto; text-indent: 13px; } </style> </head> <body> <p> <span>多图片延迟加载:</span><br/> <span>(1)多图片延迟加载;</span><br/> <span>(2)多图片延迟加载;</span><br/> <span>(3)多图片延迟加载。</span> </p> </body> <script> var data = [ { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, { src: "http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1509/06/c5/12227263_12227263_1441518066203_mthumb.jpg" , }, ]; var aDiv = document.getElementsByTagName( "div" ); var aImg = document.getElementsByTagName( "img" ); function bind() { var frg = document.createDocumentFragment(); for ( var i = 0; i < data.length; i++) { var div = document.createElement( "div" ); div.innerhtml = "<img realImg='" + data[i].src + "' alt=''/>" ; frg.appendChild(div); } document.body.appendChild(frg); frg = null ; } bind(); window.onscroll = function () { var scrollBottom = document.documentElement.scrollTop + document.documentElement.clientHeight; for ( var i = 0; i < aDiv.length; i++) { var thisImg = aImg[i]; var thisDiv = aDiv[i]; var imgPosition = thisDiv.offsetTop + thisDiv.offsetHeight; if (imgPosition - 200 < scrollBottom) { if (thisImg.isLoad) continue ; thisImg.src = thisImg.getAttribute( "realImg" ); thisImg.isLoad = true ; } } }; /* 当相关服务关闭时,登录页仍然可见,相关图片地址的配置如下 $timeout(function () { var thisImg = new Image(); thisImg.src = $scope.r_g_company.logoPage.logo.src; thisImg.onerror= function() { $scope.r_g_company.logoPage.logo.src = $scope.r_g_company.logoPage.logo.srcCY } },1000); $.get('/app_v3/oem/info?' +new Date().getTime()).then(function (res) { $scope.r_g_company.logoPage.logo.src = '/audit-html/static/img/cy_oem/logo.png'; }).catch(function(){ $scope.r_g_company.logoPage.logo.src = '/audit-html/static/img/cy_oem/logo.png'; $scope.r_g_company.logoPage.logo.srcCY = '/audit-html/static/img/cy_tech/logo.png'; }); */ </script> </html> 十五、canvas绘图 1、党徽 <!doctype html> <html> <head> <meta charset= "UTF-8" > <title>党~徽</title> </head> <body> <canvas id= "canvas" width= "450" height= "450" > 您的浏览器不支持canvas标签,无法看到党~徽 </canvas> </body> </html> <script> var canvas = document.getElementById( "canvas" ); var context = canvas.getContext( "2d" ); context.beginPath(); context.arc(215, 215, 200, 0, 360); context.fillStyle = "#030d48" ; context.strokeStyle = "grey" ; context.lineWidth = "10" ; context.stroke(); context.fill(); context.closePath(); context.beginPath(); for ( var i = 0; i < 12; i++) { context.lineTo(Math.cos((i * 30) / 180 * Math.PI) * 200 + 215, Math.sin((i * 30) / 180 * Math.PI) * 200 + 215); context.lineTo(Math.cos((i * 30 + 15) / 180 * Math.PI) * 105 + 215, Math.sin((i * 30 + 15) / 180 * Math.PI) * 105 + 215); } context.fillStyle = "#ffffff" ; context.fill(); context.closePath(); context.beginPath(); context.arc(215, 215, 105, 0, 360); context.strokeStyle = "#030d48" ; context.lineWidth = "10" ; context.stroke(); context.closePath(); </script> 2、五角星 <!doctype html> <html> <head> <meta charset= "UTF-8" > <title>五~角~星</title> </head> <body> <canvas id= "canvas" width= "450" height= "450" > 您的浏览器不支持canvas标签,无法看到五~角~星 </canvas> </body> </html> <script> var canvas = document.getElementById( "canvas" ); var context1 = canvas.getContext( "2d" ); context1.beginPath(); context1.rotate(18*Math.PI/180); for (i = 0; i < 5; i++) { context1.lineTo(Math.cos((i * 72+36) / 180 * Math.PI) * 200 + 250, Math.sin((i * 72+36) / 180 * Math.PI) * 200 + 150); context1.lineTo(Math.cos((i * 72+72) / 180 * Math.PI) * 75 + 250, Math.sin((i * 72+72) / 180 * Math.PI) * 75 + 150); } context1.fillStyle = "#ff0000" ; context1.fill(); context1.closePath(); </script> 3、时钟 这是用HTML5的canvas制作的一个钟表,包含表盘、时针、分针、秒针及它们的运动;另外还添加了自动读时间的功能。 <!doctype html> <html> <head> <style> #clock{ display:block; background:url( "" ) no-repeat; margin: 0 auto; } </style> </head> <body> <canvas id= "clock" width= "500" height= "500" > 您的浏览器不支持canvas标签,无法看到时钟 </canvas> </body> </html> <script> var clock=document.getElementById( 'clock' ); var context=clock.getContext( '2d' ); function drawClock() { context.clearRect(0, 0, 500, 500); function tow(n) { return n >= 0 && n < 10 ? '0' + n : '' + n; } var now = new Date(); var second = now.getSeconds(); var minute = now.getMinutes(); var hour = now.getHours(); var date = now.getDate(); var month = now.getMonth()+1; var year = now.getFullYear(); hour = hour + minute / 60; var hour1=hour; hour = hour > 12 ? hour - 12 : hour; //制作时钟外圈 context.lineWidth = 10; context.beginPath(); context.arc(250, 270, 200, 0, 360, false ); context.stroke(); context.closePath(); //小时刻度制作 for (i = 0; i < 12; i++) { context.save(); context.lineWidth = 9; context.strokeStyle = "red" ; context.translate(250, 270); context.rotate(i * 30 * Math.PI / 180); context.beginPath(); context.moveTo(0, -170); context.lineTo(0, -190); context.stroke(); context.closePath(); context.restore(); } //分钟刻度制作 for ( i = 0; i < 60; i++) { context.save(); context.lineWidth = 5; context.strokeStyle = "blue" ; context.translate(250, 270); context.rotate(i * 6 * Math.PI / 180); context.beginPath(); context.moveTo(0, -180); context.lineTo(0, -190); context.stroke(); context.closePath(); context.restore(); } //钟面上表示小时的数字 for ( var i = 1; i < 13; i++) { context.save(); var deg = i * Math.PI / 6; context.translate(250, 270); var x = Math.sin(deg); var y = -Math.cos(deg); context.fillStyle = "block" ; context.font = "25px 宋体" ; context.textAlign = "center" ; context.textBaseline = "middle" ; context.lineWidth=1; context.strokeStyle= "white" ; context.beginPath(); context.strokeText(i, x * 155, y * 155); context.closePath(); context.restore(); } //时针制作 context.save(); context.lineWidth=7; context.strokeStyle= "#000" ; context.translate(250,270); context.rotate(hour*30*Math.PI/180); context.beginPath(); context.moveTo(0,-110); context.lineTo(0,10); context.stroke(); context.closePath(); context.restore(); //分针制作 context.save(); context.lineWidth=5; context.strokeStyle= "#000" ; context.translate(250,270); context.rotate(minute*6*Math.PI/180); context.beginPath(); context.moveTo(0,-135); context.lineTo(0,15); context.stroke(); context.closePath(); context.restore(); //秒针制作 context.save(); //保存当前环境; //以下是秒针的主体 context.strokeStyle= "red" ; context.lineWidth=3; context.translate(250,270); context.rotate(second*6*Math.PI/180); //秒针旋转的速度 context.beginPath(); context.moveTo(0,-170); context.lineTo(0,20); context.stroke(); context.closePath(); //以上是秒针的主体,以下是时针、分针、秒针的交叉点 context.beginPath(); context.arc(0,0,5,0,360, false ); context.closePath(); context.fillStyle= "gray" ; context.fill(); context.stroke(); //以上是时针、分针、秒针的交叉点,以下是秒针的顶端装饰。 context.beginPath(); context.arc(0,-150,5,0,360, false ); context.closePath(); context.fillStyle= "gray" ; context.fill(); context.stroke(); context.restore(); //返回已保存过的环境。 //以下是文字报时; context.save(); context.font= "23px 楷体" ; context.lineWidth=1; context.strokeStyle= "white" ; context.strokeText( "现在是北京时间:" +tow(year)+ "年" +tow(month)+ "月" +tow(date)+ "日 " +tow(Math.floor(hour1))+ "时" +tow(minute)+ "分" +tow(second)+ "秒" ,5,30); context.restore(); } drawClock(); setInterval(drawClock,1000); </script> 十六、弹窗 1、弹窗-系统内置 <!DOCTYPE html> <html> <head> <meta charset= "utf-8" > </head> <body style= "font-size: 30px;" > <pre> alert: 1、弹-窗有确定按钮,阻塞线程, 2、alert后面的代码,点击确认后才执行 confirm: 1、弹-窗有两个按钮(确定和取消),阻塞线程, 2、confirm后面的代码,点击按钮后才执行, 3、点击确定返回 true ,点击取消返回 false prompt: 1、弹-窗有一个输入框、两个按钮(确定和取消),阻塞线程 2、prompt后面的代码,点击按钮后才执行, 3、点击确定返回输入内容,为空返回空字符串,点击取消返回 null </pre> <input type= "button" onclick= "fnAlert()" value= "显示alert框" /> <input type= "button" onclick= "fnConfirm()" value= "显示confirm框" /> <input type= "button" onclick= "fnPrompt()" value= "显示prompt框" /> </body> </html> <script> function fnAlert(){ var text = alert( "你好,我是一个alert框!" ); console.log( text ); } function fnConfirm(){ var text = confirm( "你好,我是一个confirm框!" ); console.log( text ); } function fnPrompt(){ var text = prompt( "你好,我是一个prompt框!" , "这是默认输入!" ); console.log( text ); } </script> 2、弹窗-手写(可拖拽和缩放) <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>弹窗-可拖拽和缩放</title> <style> * { padding: 0; margin: 0; } html,body { height: 100%; } .mask { position: fixed ; width: 100%; height: 100%; background: #bbb; } #alert { position: absolute; background: #ddd; margin: auto; width: 600px; height: 800px; left: 50%; top: 50%; transform: translate(-50%, -50%); user- select : none; resize: both; overflow: auto; } .innerBox { padding: 0 30px; } p { line-height: 40px; } .title { height: 50px; line-height: 50px; background: gray; padding-left: 30px; cursor: pointer; } .submit{ text-align: center; background: gray; display: block; margin: 0 auto; width:100px; height:30px; line-height: 30px; } </style> </head> <body> <div class = "mask" > <div id= "alert" > <p id= "alert-title-id" class = "title" > 本弹窗特征(鼠标置于此处,方可拖动): </p> <div class = "innerBox" > <p> 1、标题区可拖拽 </p> <p> 2、内容区可以是任意高度、宽度 </p> <p> 3、初始位置居中,由下面css决定 </p> <div style= "padding-left:30px;" > <p> left: 50%; </p> <p> top: 50%;</p> <p> transform: translate(-50%, -50%);</p> </div> <p> 4、弹-窗可缩放,由下面css决定 </p> <div style= "padding-left:30px;" > <p> resize: both; </p> <p> overflow: auto;</p> </div> <p> 5、提交按钮和文字居中,由下面css决定 </p> <div style= "padding-left:30px;" > <p> text-align:center; width:100px; background:gray;</p> <p> display:block; height:30px; line-height:30px; margin:0 auto;</p> </div> <p> 6、你使用时,在关闭弹-窗之前,用上面3处css代码重置弹-窗的位置,否则,下次使用弹-窗时,弹-窗将出现在上次关闭时的地方。 </p> <p> 7、弹-窗向任何方向(上下左右)拖拽,当消失3/4时,停止移动。 </p> <p> 8、拖拽弹-窗的右下方,可以实现缩放。 </p> </div> <div style= "padding-top:30px;" > <span class = "submit" >提交</span> </div> </div> </div> </body> </html> <script> var oDiv = document.getElementById( "alert" ); oDiv.onmousedown = down; function processThis(fn, currentThis) { return function ( event ) { fn.call(currentThis, event ); //”先触发,后执行“与”先执行,后触发“ }; } function down( event ) { event = event || window. event ; if ( event .target.id != "alert-title-id" ) return ; this .initOffsetLeft = this .offsetLeft; this .initOffsetTop = this .offsetTop; this .initClientX = event .clientX; this .initClientY = event .clientY; this .maxOffsetWidth = (document.documentElement.clientWidth || document.body.clientWidth) - this .offsetWidth; this .maxOffsetHeight = (document.documentElement.clientHeight || document.body.clientHeight) - this .offsetHeight; if ( this .setCapture) { this .setCapture(); this .onmousemove = processThis(move, this ); this .onmouseup = processThis(up, this ); } else { document.onmousemove = processThis(move, this ); document.onmouseup = processThis(up, this ); } } function move( event ) { var currentLeft = this .initOffsetLeft + ( event .clientX - this .initClientX); var currentTop = this .initOffsetTop + ( event .clientY - this .initClientY); //以下都是边界值的判断;弹窗向任何方向(上下左右)拖拽,当消失3/4时,停止移动。 if (currentLeft > this .maxOffsetWidth + this .clientWidth / 0.8) { currentLeft = this .maxOffsetWidth + this .clientWidth / 0.8; } else if (currentLeft < - this .clientWidth / 4) { currentLeft = - this .clientWidth / 4; } if (currentTop > this .maxOffsetHeight + this .clientHeight / 0.8) { currentTop = this .maxOffsetHeight + this .clientHeight / 0.8; } else if (currentTop < 300) { //-this.clientHeight / 4 currentTop = 300; //-this.clientHeight / 4 } //以上都是边界值的判断;弹-窗向任何方向(上下左右)拖-拽,当消失3/4时,停止移动。 //以下都是边界值的判断;弹-窗向任何方向(上下左右)拖-拽,当消失1/2时,停止移动。 /* if (currentLeft > this.maxOffsetWidth + this.clientWidth) { currentLeft = this.maxOffsetWidth + this.clientWidth; } else if (currentLeft < -this.clientWidth / 64) { currentLeft = -this.clientWidth / 64; } if (currentTop > this.maxOffsetHeight + this.clientHeight) { currentTop = this.maxOffsetHeight + this.clientHeight; } else if (currentTop < -this.clientHeight / 64) { currentTop = -this.clientHeight / 64; } */ //以上都是边界值的判断;弹-窗向任何方向(上下左右)拖-拽,当消失1/2时,停止移动。 //以下都是边界值的判断;弹-窗向任何方向(上下左右)拖-拽,触边时,停止移动。 /* if (currentLeft > this.maxOffsetWidth + this.clientWidth / 2) { currentLeft = this.maxOffsetWidth + this.clientWidth / 2; } else if (currentLeft < this.clientWidth / 2) { currentLeft = this.clientWidth / 2; } if (currentTop > this.maxOffsetHeight + this.clientHeight / 2) { currentTop = this.maxOffsetHeight + this.clientHeight / 2; } else if (currentTop < this.clientHeight / 2) { currentTop = this.clientHeight / 2; } */ //以上都是边界值的判断;弹-窗向任何方向(上下左右)拖-拽,触边时,停止移动。 this .style.left = currentLeft + "px" ; this .style.top = currentTop + "px" ; console.log( this .style.left); console.log( this .style.top); } function up() { if ( this .releaseCapture) { this .releaseCapture(); this .onmousemove = null ; this .onmouseup = null ; } else { document.onmousemove = null ; document.onmouseup = null ; } } </script> 十七、表格排序 说明,这里的表格排序,包含按姓名、年龄、分数、性别等汉字和数字的排序。用纯原生JavaScript代码实现,同时还实现了隔行变色。 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title></title> <style> #table { width: 600px; border: 3px solid darkgreen; margin: 20px auto; text-align: center; } #table tr { height: 40px; line-height: 40px; } .bg0 { background: mediumvioletred; } .bg1 { background: greenyellow; } .bg2 { background: yellow; } .cursor { cursor: pointer } </style> </head> <body> <table id= "table" class = "table" > <thead> <tr class = "bg2" > <th class = "cursor" >姓名</th> <th class = "cursor" >年龄</th> <th class = "cursor" >分数</th> <th class = "cursor" >性别</th> </tr> </thead> <tbody> </tbody> </table> </body> </html> <script> var table = document.getElementById( "table" ); var th = table.tHead.rows[0].cells; var body = table.tBodies[0]; var row = body.rows; console.log(row); console.log( '上面,展开前是空数组,这是代码执行到此的结果' ); console.log( '上面,展开后有6项数据,这是页面渲染完毕后的结果' ); var data = [ { name: "赵老大" , age: 45, score: 60, sex: 0 }, { name: "钱老二" , age: 24, score: 67, sex: 1 }, { name: "孙老三" , age: 38, score: 79, sex: 1 }, { name: "李老四" , age: 30, score: 80, sex: 0 }, { name: "周老五" , age: 65, score: 56, sex: 1 }, { name: "吴老六" , age: 26, score: 26, sex: 0 }, ]; //绑定原始数据 bind(); function bind() { var frg = document.createDocumentFragment(); for ( var i = 0; i < data.length; i++) { var cur = data[i]; var tr = document.createElement( "tr" ); for ( var attr in cur) { if (attr === "sex" ) { cur[attr] = cur[attr] === 0 ? "男" : "女" ; } var td = document.createElement( "td" ); td.innerHTML = cur[attr]; tr.appendChild(td); } frg.appendChild(tr); } body.appendChild(frg); //2、 frg = null ; } //实现隔行变色 changeColor(); function changeColor() { for ( var i = 0; i < row.length; i++) { row[i].className = "bg" + (i % 2); } } //绑定点击事件 for ( var i = 0; i < th.length; i++) { if (th[i].className === "cursor" ) { th[i].flag = -1; th[i].index = i; th[i].onclick = function () { sortArray.call( this , this .index); }; } } //类数组转化为数组 function makeArray(arg) { var ary = []; try { ary = Array.prototype.slice.call(arg); } catch (e) { for ( var i = 0; i < arg.length; i++) { ary.push(arg[i]); } } return ary; } //点击事件中的排序 function sortArray(n) { var that = this ; for ( var i = 0; i < th.length; i++) { th[i].flag = i === n ? (that.flag *= -1) : -1; } var ary = makeArray(row); ary.sort(function (rowBefore, rowBehind) { var rowInnerBefore = rowBefore.cells[n].innerHTML; var rowInnerBehind = rowBehind.cells[n].innerHTML; return rowInnerBefore.localeCompare(rowInnerBehind); }); var frg = null ; for (i = 0; i < ary.length; i++) { frg = ary[i]; body.appendChild(frg); } changeColor(); } </script> 十八、拖拽 1、拖拽-普通 <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>拖拽-普通</title> <style> *{ margin:0; padding:0; } div{ position: absolute; left:0; top:0; width: 100px; height: 100px; background: red; } </style> </head> <body> <div id= "div" ></div> </body> </html> <script> var oDiv = document.getElementById( "div" ); oDiv.onmousedown = down; function processThis(fn, obj) { return function (e) { fn.call(obj, e); }; } function down( event ) { event = event || window. event ; this .offsetLeftPass = this .offsetLeft; this .offsetTopPass = this .offsetTop; this .eventClientX = event .clientX; this .eventClientY = event .clientY; if ( this .setCapture) { this .setCapture(); this .onmousemove = processThis(move, this ); this .onmouseup = processThis(up, this ); } else { document.onmousemove = processThis(move, this ); document.onmouseup = processThis(up, this ); } } function move( event ) { event = event || window. event ; this .style.left = this .offsetLeftPass + ( event .clientX - this .eventClientX) + "px" ; //this.offsetLeftPass:移动前offsetLeft值;(event.clientX-this.eventClientX):鼠标横向移动的距离,即盒子横向移动的距离 this .style.top = this .offsetTopPass + ( event .clientY - this .eventClientY) + "px" ; //this.offsetTopPass:移动前offsetTop值;(event.clientX-this.eventClientX):鼠标纵向移动的距离,即盒子纵向移动的距离 } function up() { if ( this .releaseCapture) { this .releaseCapture(); this .onmousemove = null ; this .onmouseup = null ; } else { document.onmousemove = null ; document.onmouseup = null ; } } </script> 2、拖拽-zTree <!DOCTYPE html> <html> <head> <title>拖拽-zTree</title> <meta http-equiv= "content-type" content= "text/html; charset=UTF-8" > <link rel= "stylesheet" href= "https://cdn.bootcss.com/zTree.v3/3.5.33/css/zTreeStyle/zTreeStyle.min.css" > <script type= "text/javascript" src= "https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" ></script> <script type= "text/javascript" src= "https://cdn.bootcss.com/zTree.v3/3.5.33/js/jquery.ztree.core.min.js" ></script> <script type= "text/javascript" src= "https://cdn.bootcdn.net/ajax/libs/zTree.v3/3.5.29/js/jquery.ztree.excheck.js" ></script> <script type= "text/javascript" src= "https://cdn.bootcdn.net/ajax/libs/zTree.v3/3.5.29/js/jquery.ztree.exedit.js" ></script> <script type= "text/javascript" > var setting = { edit: { enable: true , showRemoveBtn: false , showRenameBtn: false }, data: { simpleData: { enable: true } }, callback: { beforeDrag: beforeDrag, beforeDrop: beforeDrop } }; var zNodes =[ { id:1, pId:0, name: "随意拖-拽 1" , open: true }, { id:11, pId:1, name: "随意拖-拽 1-1" }, { id:12, pId:1, name: "随意拖-拽 1-2" , open: true }, { id:121, pId:12, name: "随意拖-拽 1-2-1" }, { id:122, pId:12, name: "随意拖-拽 1-2-2" }, { id:123, pId:12, name: "随意拖-拽 1-2-3" }, { id:13, pId:1, name: "禁止拖-拽 1-3" , open: true , drag: false }, { id:131, pId:13, name: "禁止拖-拽 1-3-1" , drag: false }, { id:132, pId:13, name: "禁止拖-拽 1-3-2" , drag: false }, { id:133, pId:13, name: "随意拖-拽 1-3-3" }, { id:2, pId:0, name: "随意拖-拽 2" , open: true }, { id:21, pId:2, name: "随意拖-拽 2-1" }, { id:22, pId:2, name: "禁止拖-拽到我身上 2-2" , open: true , drop: false }, { id:221, pId:22, name: "随意拖-拽 2-2-1" }, { id:222, pId:22, name: "随意拖-拽 2-2-2" }, { id:223, pId:22, name: "随意拖-拽 2-2-3" }, { id:23, pId:2, name: "随意拖-拽 2-3" } ]; function beforeDrag(treeId, treeNodes) { for ( var i=0,l=treeNodes.length; i<l; i++) { if (treeNodes[i].drag === false ) { return false ; } } return true ; } function beforeDrop(treeId, treeNodes, targetNode, moveType) { return targetNode ? targetNode.drop !== false : true ; } function setCheck() { var zTree = $.fn.zTree.getZTreeObj( "treeDemo" ), isCopy = $( "#copy" ).attr( "checked" ), isMove = $( "#move" ).attr( "checked" ), prev = $( "#prev" ).attr( "checked" ), inner = $( "#inner" ).attr( "checked" ), next = $( "#next" ).attr( "checked" ); zTree.setting.edit.drag.isCopy = isCopy; zTree.setting.edit.drag.isMove = isMove; showCode(1, [ 'setting.edit.drag.isCopy = ' + isCopy, 'setting.edit.drag.isMove = ' + isMove]); zTree.setting.edit.drag.prev = prev; zTree.setting.edit.drag.inner = inner; zTree.setting.edit.drag.next = next; showCode(2, [ 'setting.edit.drag.prev = ' + prev, 'setting.edit.drag.inner = ' + inner, 'setting.edit.drag.next = ' + next]); } function showCode(id, str) { var code = $( "#code" + id); code.empty(); for ( var i=0, l=str.length; i<l; i++) { code.append( "<li>" +str[i]+ "</li>" ); } } $(document).ready(function(){ $.fn.zTree.init($( "#treeDemo" ), setting, zNodes); setCheck(); $( "#copy" ).bind( "change" , setCheck); $( "#move" ).bind( "change" , setCheck); $( "#prev" ).bind( "change" , setCheck); $( "#inner" ).bind( "change" , setCheck); $( "#next" ).bind( "change" , setCheck); }); </script> </head> <body> <p>参考文档1,https: //treejs.cn/v3/main.php#_zTreeInfo</p> <p>参考文档2,https: //www.bootcdn.cn/zTree.v3/3.5.29</p> <div class = "content_wrap" > <div class = "zTreeDemoBackground left" > <ul id= "treeDemo" class = "ztree" ></ul> </div> <div class = "right" > <ul class = "info" > <li class = "title" ><h2>1、setting 配置信息说明</h2> <ul class = "list" > <li>此 Demo 仅从功能上演示实现拖拽的基本方法和配置参数</li> <li class = "highlight_red" >1)、使用拖-拽功能,必须设置 setting.edit 中的各个属性,详细请参见 API 文档中的相关内容</li> <li class = "highlight_red" >2)、使用拖-拽功能的事件回调函数,必须设置 setting.callback.beforeDrag / onDrag / beforeDrop / onDrop 等属性,详细请参见 API 文档中的相关内容</li> <li><p>基本拖-拽设置:<br/> <input type= "checkbox" id= "copy" class = "checkbox first" checked /><span>允许复制</span> <input type= "checkbox" id= "move" class = "checkbox " checked /><span>允许移动</span><br/> <ul id= "code1" class = "log" style= "height:42px;" ></ul></p> </li> <li><p>拖-拽相对位置设置:<br/> <input type= "checkbox" id= "prev" class = "checkbox first" checked /><span>prev</span> <input type= "checkbox" id= "inner" class = "checkbox " checked /><span>inner</span> <input type= "checkbox" id= "next" class = "checkbox " checked /><span>next</span><br/> <ul id= "code2" class = "log" style= "height:65px;" ></ul></p> </li> </ul> </li> <li class = "title" ><h2>2、treeNode节点数据说明</h2> <ul class = "list" > <li>对节点数据没有特殊要求,用户可以根据自己的需求添加自定义属性</li> </ul> </li> </ul> </div> </div> </body> </html> 十九、设计模式 附、书籍 (1)JavaScript高级程序设计(第5版) (2)JavaScript函数式编程 (3)JavaScript设计模式 (4)JavaScript设计模式与开发实践 1、工厂模式 (1)示例 function createPerson(name, age) { // 定义一个工厂函数 // 返回一个新对象 return { name: name, age: age, greet: function() { console.log(`Hello, my name is ${ this .name} and I am ${ this .age} years old.`); } }; } // 使用工厂函数创建对象 const person1 = createPerson( 'Alice' , 30); const person2 = createPerson( 'Bob' , 25); // 调用对象的方法 person1.greet(); // 输出: Hello, my name is Alice and I am 30 years old. person2.greet(); // 输出: Hello, my name is Bob and I am 25 years old. (2)实现重置 //工厂函数,用于创建对象 function createUser(name, age) { //初始状态 const initialState = { name: name, age: age }; //创建一个对象 const user = { ...initialState, //重置方法 reset: function() { Object.assign( this , initialState); } }; return user; } //创建一个用户对象 const user = createUser( 'Alice' , 25); console.log(user); //{ name: 'Alice', age: 25, reset: [Function: reset] } //修改用户对象的属性 user.name = 'Bob' ; user.age = 30; console.log(user); //{ name: 'Bob', age: 30, reset: [Function: reset] } //重置用户对象的属性 user.reset(); console.log(user); //{ name: 'Alice', age: 25, reset: [Function: reset] } 2、单例模式 (1)示例 const Singleton = (function () { //私有变量,用于存储单例实例 let instance; //私有方法,用于创建实例 function createInstance() { const object = new Object( "I am the instance" ); return object ; } return { //获取单例实例的公共方法 getInstance: function () { if (!instance) { instance = createInstance(); } return instance; } }; })(); (2)使用 const instance1 = Singleton.getInstance(); const instance2 = Singleton.getInstance(); console.log(instance1 === instance2); //输出: true,因为两个变量引用的是同一个实例 |
« 上一篇: 4、主机构成、浏览器之进程|线程|执行栈|工作流程、性能检测分析、后端缓存http、前端优化、前端缓存(如cookie、storage、vuex等)、长连接(如worker、Socket等)、BOM(DOM方法事件区别|复制|clientRect)、九宫格、瀑布流、跑马灯、卷轴展开、几字选项卡、胶卷动画、放大镜、git、gitLab、VSCode、jinja2语法(4150行)
» 下一篇: 6、轮播图之rem|原生|jQuery、table表格(合并嵌套、固定栏、滑动栏、滚动条)、js画曲线、验证码 、发电报、css汇总(光标、改查、滚动条、兄弟叠加、tabIndex和a标签、清除浮动、悬停改样式、媒体查询、BEM、栅格布局、图片撑开盒子、变量--var、密码禁用、半包围、显式隐式点击事件、旋转正方体、太极图、单词破单词裹空白省略号、符号图标、伪类伪元素、三角形箭头)(2700行)
» 下一篇: 6、轮播图之rem|原生|jQuery、table表格(合并嵌套、固定栏、滑动栏、滚动条)、js画曲线、验证码 、发电报、css汇总(光标、改查、滚动条、兄弟叠加、tabIndex和a标签、清除浮动、悬停改样式、媒体查询、BEM、栅格布局、图片撑开盒子、变量--var、密码禁用、半包围、显式隐式点击事件、旋转正方体、太极图、单词破单词裹空白省略号、符号图标、伪类伪元素、三角形箭头)(2700行)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构