日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
29 個有用的 JavaScript 單行代碼

今天這篇文章,我主要是想跟大家分享一些關(guān)于JavaScript的單行代碼技巧,在這些方法中,我們使用了一些API,幫助我們簡化操作,可能有些方法寫一行不是很優(yōu)雅,我們這么做的目的,主要是為了進(jìn)一步學(xué)習(xí)使用API的技巧,希望對你的學(xué)習(xí)有所幫助。

創(chuàng)新互聯(lián)公司是專業(yè)的資源網(wǎng)站建設(shè)公司,資源接單;提供網(wǎng)站制作、做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行資源網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

現(xiàn)在,我們就開始進(jìn)入今天的內(nèi)容。

1.復(fù)制內(nèi)容到剪貼板

const copyToClipboard = (text) => navigator.clipboard.writeText(text);
copyToClipboard("Hello World");

2.清除所有cookie

const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`));

3.獲取選中的文字

const getSelectedText = () => window.getSelection().toString();
getSelectedText();

4.滾動到頁面頂部

const goToTop = () => window.scrollTo(0, 0);
goToTop();

5.判斷當(dāng)前tab是否激活

const isTabInView = () => !document.hidden;

6.判斷當(dāng)前設(shè)備是否為蘋果設(shè)備

const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform);
isAppleDevice();

7.是否滾動到頁面底部

const scrolledToBottom = () => document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight;

8. 重定向到一個 URL

const redirect = url => location.href = url
redirect("https://www.google.com/")

9.打開瀏覽器打印框

const showPrintDialog = () => window.print()

10.隨機(jī)布爾

const randomBoolean = () => Math.random() >= 0.5;
randomBoolean();

11.變量交換

[foo, bar] = [bar, foo];

12.獲取變量的類型

const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
trueTypeOf(''); // string
trueTypeOf(0); // number
trueTypeOf(); // undefined
trueTypeOf(null); // null
trueTypeOf({}); // object
trueTypeOf([]); // array
trueTypeOf(0); // number
trueTypeOf(() => {}); // function

13.檢查對象是否為空

const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;

14.檢查日期是否有效

const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());
isDateValid("December 17, 2022 03:24:00");

15.計(jì)算兩個日期之間的間隔

const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)

dayDif(new Date("2022-11-3"), new Date("2023-2-1"));

16. 找出日期所在年份中的第幾天

const dayOfYear = (date) => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
dayOfYear(new Date());

17.時間格式化

const timeFromDate = date => date.toTimeString().slice(0, 8);

timeFromDate(new Date(2022, 11, 2, 12, 30, 0));
timeFromDate(new Date());

18.將字符串首字母大寫

const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)
capitalize("hello world")

19.翻轉(zhuǎn)字符串

const reverse = str => str.split('').reverse().join('');
reverse('hello world');

20.隨機(jī)字符串

const randomString = () => Math.random().toString(36).slice(2);
randomString();

21. 截斷字符串

const truncateString = (string, length) => string.length < length ? string : `${string.slice(0, length - 3)}...`;
truncateString('Hi, I am too loooong!', 12);

22. 從字符串中刪除 HTML

const stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';
23. 刪除數(shù)組中的重復(fù)項(xiàng)
const removeDuplicates = (arr) => [...new Set(arr)];
console.log(removeDuplicates([1, 2, 2, 3, 3, 4, 4, 5, 5, 6]));

24.檢查數(shù)組是否為空

const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0;
isNotEmpty([1, 2, 3]);

25.合并兩個數(shù)組

const merge = (a, b) => a.concat(b);
const merge = (a, b) => [...a, ...b];

26.判斷一個數(shù)是奇數(shù)還是偶數(shù)

const isEven = num => num % 2 === 0;
isEven(1024);

27. 求一組數(shù)的平均值

const average = (...args) => args.reduce((a, b) => a + b) / args.length;
average(1, 2, 3, 4, 5);

28. 獲取兩個整數(shù)之間的隨機(jī)整數(shù)

const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
random(1, 50);

29.四舍五入到指定位數(shù)

const round = (n, d) => Number(Math.round(n + "e" + d) + "e-" + d)
round(1.005, 2);
round(1.555, 2);

寫在最后

以上就是我今天整理的29個關(guān)于JavaScript的單行代碼技巧,希望這些技巧對你有用,如果你覺得有幫助的話,請點(diǎn)贊我,關(guān)注我,這樣,你將會獲取到更多有價值的內(nèi)容與信息。

最后,感謝你的閱讀,快樂學(xué)習(xí),開心編程。


網(wǎng)站名稱:29 個有用的 JavaScript 單行代碼
網(wǎng)頁網(wǎng)址:http://m.5511xx.com/article/cdojdss.html