改字級只需要一行 css():
$("#hovertree").css("font-size", "50px");
這行的意思是把 id 為 hovertree 的元素字級設為 50px。
<div>
<span id="hovertree">
這是文字內容,何問起, hovertree.com
</span>
<button id="btnhovertree">設置 span 的 font-size 為 50px</button>
</div>
<script src="https://down.hovertree.com/jquery/jquery-1.12.3.min.js"></script>
<script>
$("#btnhovertree").on("click", function () {
$("#hovertree").css("font-size", "50px");
});
</script>
<!DOCTYPE html>
<html>
<head>
<title>jQuery 設置 font-size - 何問起</title>
<style>
#hovertree { font-size: 16px; }
</style>
</head>
<body>
<div>
<span id="hovertree">這是文字內容,何問起, hovertree.com</span>
<button id="btnhovertree">設置 span 的 font-size 為 50px</button>
</div>
<script src="https://down.hovertree.com/jquery/jquery-1.12.3.min.js"></script>
<script>
$("#btnhovertree").on("click", function () {
$("#hovertree").css("font-size", "50px");
});
</script>
</body>
</html>
css("font-size", "50px") 可以,直接寫數字 50 在部分情況下會被補上 px,但遇到 line-height 這類無單位屬性時就容易出錯,養成寫單位的習慣比較安全。css("fontSize", "50px") 與 css("font-size", "50px") 等效,jQuery 會自動轉換。em、rem 或 % 會比固定 px 好維護。$("#hovertree").css({ "font-size": "50px", color: "#f60" })。