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。
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
主题授权提示:请在后台主题设置-主题授权-激活主题的正版授权,授权购买:RiTheme官网
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。