Set the font for a <p> element:
document.getElementById("myP").style.fontFamily = "Impact,Charcoal,sans-serif";
<!DOCTYPE html> <html> <body> <p id="myP">This is a paragraph.</p> <button type="button" onclick="myFunction()">Set font</button> <script> function myFunction() {/*www . ja va 2s . co m*/ document.getElementById("myP").style.fontFamily = "Impact,Charcoal,sans-serif"; } </script> </body> </html>
The fontFamily property sets or gets a list of font-family names and/or generic-family names for text in an element.
The browser will use the first value it recognizes.
There are two types of font-family values:
verdana
or arial
You should always specify a generic-family name as the last alternative!
Separate each value with a comma.
If a font-family name contains whitespace, it must be quoted.
Property Values
Value | Description |
---|---|
font1, font2, etc. | A comma-separated list of font-family names and/or generic-family names |
initial | Sets this property to its default value. |
inherit | Inherits this property from its parent element. |
The fontFamily property returns a String representing the font name of the text in the element.