获得当前字符串的宽度
生活随笔
收集整理的這篇文章主要介紹了
获得当前字符串的宽度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>獲得當前字符串的寬度</title>
<script type="text/javascript" src="jquery-min.js"></script>
</head>
<body>
<span id="span" style="font-size:14px">china中國人的奧運</span>
</body>
</html>
<script>
//var str='china中國人的奧運';
function textSize(fontSize, text) {
var span = document.createElement("span");
var result = {};
result.width = span.offsetWidth;
result.height = span.offsetHeight;
span.style.visibility = "hidden";
document.body.appendChild(span);
if (typeof span.textContent != "undefined")
span.textContent = text;
else span.innerText = text;
result.width = span.offsetWidth - result.width;
result.height = span.offsetHeight - result.height;
span.parentNode.removeChild(span);
return result;
}
var size = textSize("14px", "china中國人的奧運");
alert("寬:" + size.width + " 高:" + size.height);
//當前字符串的長度
alert("寬:" + span.offsetWidth + " 高:" + span.offsetHeight);
</script>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>獲得當前字符串的寬度</title>
<script type="text/javascript" src="jquery-min.js"></script>
</head>
<body>
<span id="span" style="font-size:14px">china中國人的奧運</span>
</body>
</html>
<script>
//var str='china中國人的奧運';
function textSize(fontSize, text) {
var span = document.createElement("span");
var result = {};
result.width = span.offsetWidth;
result.height = span.offsetHeight;
span.style.visibility = "hidden";
document.body.appendChild(span);
if (typeof span.textContent != "undefined")
span.textContent = text;
else span.innerText = text;
result.width = span.offsetWidth - result.width;
result.height = span.offsetHeight - result.height;
span.parentNode.removeChild(span);
return result;
}
var size = textSize("14px", "china中國人的奧運");
alert("寬:" + size.width + " 高:" + size.height);
//當前字符串的長度
alert("寬:" + span.offsetWidth + " 高:" + span.offsetHeight);
</script>
轉載于:https://www.cnblogs.com/cx709452428/p/5763597.html
總結
以上是生活随笔為你收集整理的获得当前字符串的宽度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jmeter文章索引贴
- 下一篇: 简单的Flash GUI工具(Simpl