JavaScript bold method entry instance (displays the specified text in bold)

  • 2020-03-30 04:09:04
  • OfStack

JavaScript bold method

The lastIndexOf method returns a bold string defined using an HTML b tag. The syntax is as follows:


str_object.bold()

Note: this method does not conform to ECMA standards and is not recommended.

Bold method instance


<script language="JavaScript"> var str = "www.jb51.net";
document.write(str.bold()); </script>

Run the example and output:


www.jb51.net

Tip: this method returns a bold string defined using an HTML b tag, meaning that you cannot dynamically change the font of a page element to bold using this method. If you want to change the element font to bold dynamically, see the following example:

Extended reading: change the font size of page elements


<html> <script language="JavaScript"> function changFont( x ){
    var font_style = x;
    var article = document.getElementById("article");
    if( typeof font_style == "string" ){
        article.style.fontWeight = font_style;
    } else {
        article.style.fontSize = font_style;
    }
} </script> <body>
<p>
<a onClick="changFont('normal');"> normal </a> <a onClick="changFont('bold');"> The bold </a>
<a onClick="changFont(14);"> The trumpet </a> <a onClick="changFont(18);"> large </a>
</p>
<p id="article">
I am some words ...<br />
Some Text ...
</p>
</body>
</html>

In this example, the way JavaScript controls the CSS style of the font allows the display font (id="article") to switch between bold, normal, small, and large fonts in real time.


Related articles: