admin管理员组

文章数量:1193739

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?

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?

Share Improve this question edited Dec 20, 2010 at 14:14 Donut 113k20 gold badges135 silver badges147 bronze badges asked Dec 20, 2010 at 14:13 SquarefingerSquarefinger 1011 gold badge2 silver badges5 bronze badges 6
  • 5 My advice would be to find a more recent code fragment to use. Use of the <font> tag et al. has been deprecated for ages. Also, checking for Netscape is not really required anymore either... could you tell us what exactly you want to do, maybe someone can get you some modern code to do it ;) – Spiny Norman Commented Dec 20, 2010 at 14:19
  • 3 Not to mention using the language attribute on script tags... And then there's the implicit global variable. OMG, and the eval s. Yeah, this code has slipped through a time machine from 1998. Strongly recommend looking for something more recent. – T.J. Crowder Commented Dec 20, 2010 at 14:19
  • @Spiny Norman: I think QA is new to HTML and he is reading kind of an old HTML tutorial. – user142019 Commented Dec 20, 2010 at 14:23
  • @Fred: Apologies if Spiny and I have the wrong end of the stick. We've assumed you've grabbed or inherited this code from somewhere, not written it yourself. If the latter, it would be easy to be offended by our comments. Apologies if so, not how they were meant. – T.J. Crowder Commented Dec 20, 2010 at 14:30
  • @Time Machine Yeah, I'm trying to find a nice piece of code to impose a character limit that is no more than a year old, but I can't find anything with no jQuery in it ;) – Spiny Norman Commented Dec 20, 2010 at 14:34
 |  Show 1 more comment

2 Answers 2

Reset to default 20

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");

}

本文标签: javascriptChanging the Font and font Size in a textarea fieldStack Overflow