简体   繁体   中英

Changing the Font and font Size in a textarea field

Using the following code sample:

<html>
<body>

<script language = "Javascript">

maxL=240;
var bName = navigator.appName;
function taLimit(taObj) {
 if (taObj.value.length==maxL) return false;
 return true;
}

function taCount(taObj,Cnt) { 
 objCnt=createObject(Cnt);
 objVal=taObj.value;
 if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
 if (objCnt) {
  if(bName == "Netscape"){ 
   objCnt.textContent=maxL-objVal.length;}
  else{objCnt.innerText=maxL-objVal.length;}
 }
 return true;
}
function createObject(objId) {
 if (document.getElementById) return document.getElementById(objId);
 else if (document.layers) return eval("document." + objId);
 else if (document.all) return eval("document.all." + objId);
 else return eval("document." + objId);
}
</script>
<font face="Arial" font size="2">
Maximum Number of characters for this text box is 240.</font><br>

<textarea onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')" name="Message" rows=6 wrap="physical" cols=42>
</textarea>
<br>
<font face="Arial" font size="2">
You have <B><SPAN id=myCounter>240</SPAN></B> characters remaining 
for your message</font>

</body>
</html>

How do I change the font and font size in the textarea?

Use CSS to define a style for the textarea element. For example:

textarea {
   font-size: 20pt;
   font-family: Arial;
} 

Note that there are three ways to add CSS to your website -- you can use an external stylesheet, an internal stylesheet, or inline styles (or a combination thereof).

Using an internal stylesheet might look like this:

<head>
<style type="text/css">
textarea {
   font-size: 20pt;
   font-family: Arial;
} 
</style>
</head>

<!-- rest of your code goes here... -->

See here for more information on styling textarea .

I know this is really old, but I am sure someone could use this information:

use Iframe and initialize it to run when the page is loaded and hide textarea...this helps if you want to make a WYSIWYG editor.

html page: onLoad="iFrame()"

js page: function iFrame(){ name.document.designMode = 'on'; name.document.execCommand('fontName',false,"verdana");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM