【问题标题】:Make some chars of a variable in bold text [closed]用粗体文本制作变量的一些字符[关闭]
【发布时间】:2014-12-22 15:54:11
【问题描述】:

我想知道如何在 javascript 中更改变量的某些字符的样式。 例如:

var example = 'Hello World';

我希望从 char H 到 L 以粗体文本返回,其余正常。

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我试过了,但似乎不起作用:example.substring(0,2).style.color='#f00';
  • "从 H 到 L" 你的字符串中有 3 个 L。您可以使用 string 或 CSS。

标签: javascript text substring


【解决方案1】:
  1. 您可以指定您所说的“L”。
  2. 字符串 var 只存储一些字符,而不是样式。样式是用 html/css 制作的,而不是 javascript
  3. 你可以在互联网上找到你想要的,但这里是一个带有粗体文本的 html/css 示例

.boldText {
    font-weight: bold;
  }
<span class="boldText">Hel</span>lo World
  1. 不要害怕投反对票,因为在寻求解决方案之前,您应该向我们展示您尝试过的方法。

【讨论】:

  • 谢谢阿波罗:)。 Itried this example.substring(0,2).style.color='#f00';
  • 样式属性适用于 HTMLElements。您可以通过document.getElement[...] 方法获取 HTMLElements。我的代码示例:document.getElementsByClassName("boldText")[0].style.color='#F00'; 它会将粗体文本设置为红色。
  • 谢谢你的朋友,这正是我正在寻找的
【解决方案2】:

您希望在 HTML 中使用 粗体,因此:

example = "<b>Hell</b>o World";

【讨论】:

  • 注(发件人:Mozilla Developer Network):Historically, the &lt;b&gt; element was meant to make text boldface. Styling information has been deprecated since HTML4, so the meaning of the &lt;b&gt; element has been changed.If there is no semantic purpose on using the &lt;b&gt; element, using css property font-weight with bold value would be a better choice for making text bold.
猜你喜欢
  • 2016-06-23
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 2016-10-11
相关资源
最近更新 更多