前端开发中这些小技巧,你知道几个?
浏览器地址栏运行JavaScript代码
- javascript:alert('hello!');
- Firefox不支持;
- 其他浏览器必须手动输入才可执行;
浏览器地址栏运行HTML代码
- data:text/html,
Hello, world!
- 在非IE浏览器可以运行
浏览器变成文本编辑器
地址栏输入:

- data:text/html,
控制台输入:
- document.body.contentEditable='true';
利用a标签自动解析URL
- var a = document.createElement('a');
- a.href = 'https://www.maomin.club';
- console.log(a.host); // www.maomin.club
页面拥有ID的元素会创建全局变量
我是内容- console.log(app.textContent) // 我是内容
引用CDN文件时,不用HTTP标识
利用script标签保存任意信息
This won't display
- const text = document.getElementById('template').textContent;
- console.log(text); //
This won't display
在页面中隐藏鼠标
- *{
- cursor: none!important;
- }
文字模糊效果
- p {
- color: transparent;
- text-shadow: #111 0 0 5px;
- }
实时编辑CSS
- style{
- display: block;
- }
- body { color: blue }
实现长宽比固定区域
- this content will have a constant aspect ratio that varies based on the width.
禁止他人使用iframe标签引用网址
- if (window.location != window.parent.location) window.parent.location = window.location;
生成随机字符串
- function generateRandomAlphaNum(len) {
- var rdmString = "";
- for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
- return rdmString.substr(0, len);
- }
浮点数转化为整数
- console.log(~~1.2); // 1
本文名称:前端开发中这些小技巧,你知道几个?
路径分享:http://www.jxjierui.cn/article/cdeopsg.html


咨询
建站咨询
