[英]using javascipt to change the size of a font inside the text editor
我必須通過文本編輯器在應用程序中創建一些內容,但該編輯器沒有更改字體大小的選項。
但是,文本編輯器允許在其中插入 HTML 代碼。
我想使用 JS 來做到這一點,但我一直堅持如何找到正確的方法來做到這一點。
我的問題是如何增加font size of all the text inside the p
?
這是已經在文本編輯器中的html
片段。
//this is what i tried but didn't work document.getElementsByTagName("html")[0].style["font-size"] = "20px"
<p></p> <h5><b><span><span><span><i>-8 minutes</i></span></span><span> read</span></span></b></h5> <p></p> <p>Here we will see some of the examples.<br><br></p> <h4><b>Example 1:</b></h4> <p></p>
要放大所有段落,您可以執行以下操作。
需要注意的事項包括:
script
元素出現在另一個 HTML 之后(通常在body
元素內。)getElementsByTagName
選擇器返回p
類型的所有元素。for...of
語法讓我們可以遍歷段落。font-size
屬性表示為fontSize
屬性。 <p></p> <h5><b><span><span><span><i>-8 minutes</i></span></span><span> read</span></span></b></h5> <p></p> <p>Here we will see some of the examples.<br><br></p> <h4><b>Example 1:</b></h4> <p></p> <script> let paragraphs = document.getElementsByTagName("p"); for (let p of paragraphs) { p.style.fontSize = "20px"; } </script>
順便說一句,更好的做法是設置 class 而不是直接更改樣式屬性,例如:
p.classList.add("my-big-class");
在您的 CSS(可以在 HTML 的head
元素內的style
元素中)中,您定義 class 的行為,例如:
.my-big-class{ font-size: 20px; } /* CSS syntax is a bit different */
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.