簡體   English   中英

使用 javascipt 在文本編輯器中更改字體的大小

[英]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片段。

這就是生成的 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>&nbsp;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>

要放大所有段落,您可以執行以下操作。

需要注意的事項包括:

  1. script元素出現另一個 HTML 之后(通常在body元素內。)
  2. getElementsByTagName選擇器返回p類型的所有元素。
  3. for...of語法讓我們可以遍歷段落。
  4. font-size屬性表示為fontSize屬性。

 <p></p> <h5><b><span><span><span><i>-8 minutes</i></span></span><span>&nbsp;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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM