Today, I encountered the problem of obtaining the text width. After a long time, I finally found a method on a foreign website, but I couldn't use it directly. So I modified it and successfully used it in the project, here we will share with you.
First, add a sub-tag at the end of the body Tag:
Test
Then add the corresponding css code:
# Rility {visibility: hidden; white-space: nowrap; font-size: 24px ;}
Next, add the function to get the text width in the String prototype, and add the following code to the js Code:
String. prototype. equalallength = function () {var r0000=$ ("# r0000"); r0000. text (this); return r0000[ 0]. offsetWidth ;}
Finally, you can call it where you need to obtain the text width. For example:
Var text = "test"; var len = text. visualLength ();
The main idea is to add a hidden tag. After each value is assigned to the tag, the text width is obtained by obtaining the length of the tag. Note that only labels added to the DOM can obtain the length.
If you feel helpful to yourself, I hope you can help me with it. Thank you :) my blog: Sorry!