1.如果你需要一个临时的唯一 IDjs转字符串,请生成随机字符串。

这个例子将为你生成一个随机字符串:

const randomString = Math.random().toString(36).slice(2);
console.log(randomString); //output- r0zf1xfqcr (the string will be random )

2. 从电子邮件中提取域名,

你可以使用 substring() 方法来提取电子邮件的域名。

let email = 'xyz@gmail.com';
le getDomain = email.substring(email.indexOf('@') + 1);
console.log(getDomain); // output - gmail.com

3.用这个例子检测暗模式js转字符串,你可以检查用户是否在使用暗模式(然后你可以根据暗模式更新一些功能)

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').match;

4. 检查元素是否被聚焦

在JavaScript中检测元素是否具有焦点,可以使用Document对象的只读属性activeElement。

const elem = document.querySelector(' .text-input');
const isFocus = elem == document.activeElemnt;
/* isFocus will be true if elem will have focus, and isFocus will be false if elem will not have focus */

5. 检查数组是否为空

此单行程序将让你知道数组是否为空。

let arr1 = [];
let arr2 = [2, 4, 6, 8, 10];
const arr1IsEmpty = !(Array.isArray(arr1) && arr1.length >0);
const arr2IsEmpty = !(Array.isArray(arr2) && arr2.length >0);
console.log(arr1); //output - true
console.log(arr2); // output - false

6. 重定向

你可以使用 JavaScript 将用户重定向到任何特定的 URL。

js转字符串_js字符窜转数组_js字符转数字

const redirect = url => location.href = url
/* call redirect (url) whenever you want to redirect the user to a specific url */

7. 检查变量是否为数组

你可以使用 Array.isArray() 方法检查任何变量是否为数组。

let fruit = 'apple';
let fruits = ["apple", "banana", "mango", "orange", "grapes"];
const isArray = (arr) => Array.isArray(arr);
console.log(isArray.(fruit)); //output - false
console.log(isArray.(fruits)), //output- true

限时特惠:本站每日持续更新海量设计资源,一年会员只需29.9元,全站资源免费下载
站长微信:ziyuanshu688