前端开发中这些小技巧

浏览器变成文本编辑器

地址栏输入:

data:text/html, <html contenteditable>

利用a标签自动解析URL

var a = document.createElement('a');
a.href = 'https://www.maomin.club';
console.log(a.host); // www.maomin.club

页面拥有ID的元素会创建全局变量

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<div id="app">我是内容</div>
<script type="text/javascript">
	console.log(app.textContent) // 我是内容
</script>
</body>
</html>

引用CDN文件时,不用HTTP标识

<script src="//cdn.bootcdn.net/ajax/libs/vue/3.0.11/vue.cjs.js"></script>

利用script标签保存任意信息

<script type="text" id="template">
	<h1>This won't display</h1>
</script>
<script type="text/javascript">
	const text = document.getElementById('template').textContent;
	console.log(text); // <h1>This won't display</h1>
</script>

在页面中隐藏鼠标

*{
    cursor: none!important;
}

文字模糊效果

p {
    color: transparent;
    text-shadow: #111 0 0 5px;
}

实时编辑CSS

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
		style{
			display: block;
		}
	</style>
</head>
<body>
    <style  contentEditable>
        	body { color: blue }
    </style>
</body>
</html>

实时编辑html

<!DOCTYPE HTML>
<html>
<body>

<p contenteditable="true">这是一段可编辑的段落。请试着编辑该文本。</p>

</body>
</html>

实现长宽比固定区域

<div style="width: 100%; position: relative; padding-bottom: 56.25%;">
    <div style="position: absolute; left: 0; top: 0; right: 0; bottom: 0;background-color:yellow;">
        this content will have a constant aspect ratio that varies based on the width.
    </div>
</div>

禁止他人使用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值