Calculating text width

后端 未结 22 1330
轻奢々
轻奢々 2020-11-22 03:36

I\'m trying to calculate text width using jQuery. I\'m not sure what, but I am definitely doing something wrong.

So, here is the code:

var c = $(\'.c         


        
22条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 03:59

    text width can be different for different parents, for example if u add a text into h1 tag it will be wider than div or label, so my solution like this:

    alert(calcTextWidth("bir iki", $("#header1"))); function calcTextWidth(text, parentElem){ var Elem = $("").css("display", "none").text(text); parentElem.append(Elem); var width = Elem.width(); Elem.remove(); return width; }

提交回复
热议问题