JavaScript sub method entry instance (displays a string as a subscript)

  • 2020-03-30 04:08:51
  • OfStack

JavaScript sub way

The sub method returns the subscript string defined using the HTML sub tag attribute. The syntax is as follows:


str_object.sub()

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

Sub method instance


<script language="JavaScript">
document.write( ' The home of the script ' + ' Every bit of progress is a solid footprint on our road to success '.sub() );
</script>

Run the example and output:


The home of the script Every bit of progress is a solid footprint on our road to success

Tip: this method returns the subscript string defined using the HTML sub tag, even though it does not directly change the string to the subscript style. If you want to change the element font to subscript dynamically, see the following example:

Change the way the text is aligned up and down (superscript/subscript)


<html>
<script language="JavaScript">
function changFont( x ){
    document.getElementById("sub_title").style.verticalAlign = x;
}
</script>
<body>
<p>
<a onClick="changFont('sub');"> The subscript style </a> <a onClick="changFont('super');"> Superscript style </a>
</p>
<p>
The home of the script <span id="sub_title"> Every bit of progress is a solid footprint on our road to success </span>
</p>
</body>
</html>

In this example, JavaScript controls the way the text CSS is styled vertical-align to dynamically change the up and down alignment (superscript/subscript) of the font text.


Related articles: