JavaScript如何精準的獲取文字寬高?紅塵匆匆過客夢2018-05-21 21:31:52

可使用getBoundingClientRect()函式,例如html文件中標籤

some text 一些示例文字

js程式碼如下

var p = document。documentElement。querySelector(‘#demo’);

var rect = p。getBoundingClientRect();

var width = rect。right - rect。left;

var height = rect。bottom - rect。top;

alert(“width=” + width + “ height=” + height);