Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the solutions to common JavaScript problems?

Shulou Source: shulou.com Published: 2022-06-03 08:14:22 09月28日 Update

In this issue, the editor will bring you what the solutions to common JavaScript problems are. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Use JavaScript to make the window full screen

We can use full-screen API to make the window full-screen.

For example, we can write like this:

Document.documentElement.requestFullscreen ()

We just call the requestFullScreen method to change the window to full screen mode.

We can then call document.exitFullScreen to exit full-screen mode.

The fastest way to convert JavaScript NodeList to an array

We can use the extension operator (.) Or the Array.from method converts the JavaScript NodeList to an array.

For example, we can write like this:

Const els = Array.from (document.querySelectorAll ('p'))

We can use the propagation operator in the following ways:

Const els = [. Document.querySelectorAll ('p')]

Capitalize the first letter of each word

We can use some array methods to capitalize the first letter of each word.

For example, we can write like this:

Str = str.toLowerCase () .split ('') .map ((s) = > `${s.charAt (0). ToUpperCase ()} ${s.substring (1)}`) .join ('')

We first convert the string to lowercase, and then split the words using split.

Then we call map to match each word with the first letter uppercase and the rest lowercase.

Finally, we call join to put the words back together.

Verification of JavaScript file upload size

We can verify the size of the file without using any libraries.

For example, we can write like this:

Tags: Array method element attribute file word size operation remainder multiple uppercase letter number operator selection validation common solution problem Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Apple Redmi NVidia