replace函数真强大
更新日期:
写了一个把样式表的px转换成rem的东西,因为上周写的页面在平安银行APP里面显示有问题,
这个meta不识别的,
还是得用rem布局,就写了个这个,后期闲下来试试写个node或者gulp插件来做这个东西1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="text" id="con"/><input type="button" id="btn" value="开始"/>
<div id="res"></div>
<script>
document.querySelector('#btn').addEventListener('click', function () {
var txt = document.querySelector('#con').value;
var reg = /(\d+\.\d+)px|(\d+)px/ig;
function med() {
console.log(arguments);
var s1 = arguments[0];
return parseFloat(s1) / 100 + 'rem'
}
var txt2 = txt.replace(reg, med);
console.log(txt2);
document.querySelector('#res').innerHTML = txt2;
})
</script>
</body>
</html>