繁体   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