Introduction 】

In the development, some code segments are used very frequently, or some are only used occasionally, but the attributes are difficult to remember, so here it makes a summary for easy reference.

[Remove Spaces]

[JavaScript]

Plain text view
Copy the code

?
1
2
3
4
5
function
returnNoSpace(str) {

return
str.replace(/\s+/g,
' '
);
}

Compatibility scrollbar

[JavaScript]

Plain text view
Copy the code

?
1
document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop

js

Timestamp, millisecond formatting

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function
formatDate(now) {

var
y = now.getFullYear();

var
m = now.getMonth() + 1;
// Note that the month in js is incremented by 1

var
d = now.getDate();

var
h = now.getHours();

var
m = now.getMinutes();

var
s = now.getSeconds();

return
y +
"-"
+ m +
"-"
+ d +
""
+ h +
":"
+ m +
":"
+ s;
}
var
nowDate =
new
Date(2016, 5, 13, 19, 18, 30, 20);
console.log(nowDate.getTime());
// Get the current number of milliseconds: 1465816710020
console.log(formatDate(nowDate));

js

The number of qualified characters (note: one Chinese character counts

2

A character) 】

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<input id=
"txt"
type=
"text"
>
// String interception
function
getByteVal(val, max) {

var
returnValue =
' '
;

var
byteValLen = 0;

for
(
var
i = 0; i < val.length; i++) {

if
(val[i].match(/[^\x00-\xff]/ig) ! =
null
) byteValLen += 2;
else
byteValLen += 1;

if
(byteValLen > max)
break
;

returnValue += val[i];

}

return
returnValue;
}
$(
'#txt'
).on(
'keyup'
.
function
() {

var
val =
this
.value;

if
(val.replace(/[^\x00-\xff]/g,
"* *"
).length > 14) {

this
.value = getByteVal(val, 14);

}
});

The custom

dom

Event 】

[JavaScript]

Plain text view
Copy the code

?
1
2
3
4
5
6
7
8
9
var
evt = document.createEvent(
"HTMLEvents"
);

// Initialization, event type, whether to bubble, whether to block the browser's default behavior

evt.initEvent(
"tap"
.
false
.
false
);

/ / triggers

div.dispatchEvent(evt);

【 set

rem

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function
setHTML() {

/ / basic values

var
baseVal = 100;

// Width of the design draft

var
pageWidth = 375;

// What screen width to fit?

var
screenWidth = document.querySelector(
"html"
).offsetWidth;

// The fontsize to set

var
fontsize = screenWidth * baseVal / pageWidth;

// Set it to an HTML tag

document.querySelector(
"html"
).style.fontSize = fontsize +
"px"
;

}

HTML5

Full screen.

[JavaScript]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function
fullscreen(element) {

if
(element.requestFullscreen) {

element.requestFullscreen();

}
else
if
(element.mozRequestFullScreen) {

element.mozRequestFullScreen();

}
else
if
(element.webkitRequestFullscreen) {

element.webkitRequestFullscreen();

}
else
if
(element.msRequestFullscreen) {

element.msRequestFullscreen();

}
}
fullscreen(document.documentElement);


For more technical information: Gzitcast