最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript change fontsize not working everywhere - Stack Overflow

programmeradmin1浏览0评论

I have that code:

function change_npsize()
{
   document.getElementById("np_drag").style.fontSize = document.getElementsByName("npsize").item(0).value;
};


<input type="text" name="npsize" size="2" maxlength="2" value="<?=$userinfo->npsize; ?>" onchange="change_npsize()" />

<div id="drag-container" style="position:relative;font-family:<?=$userinfo->font?>;">
    <div id="np_drag" style="color:<?=$userinfo->npcolor?>; font-size:<?=$userinfo->npsize?>px;" class="draggable np_drag" style="position:absolute;left:80px;">
     .::[ NowPlaying SIGnature ]::.
     </div>
</div>

That code is working only in IE. I tried Firefox and Google Chrome.

I have that code:

function change_npsize()
{
   document.getElementById("np_drag").style.fontSize = document.getElementsByName("npsize").item(0).value;
};


<input type="text" name="npsize" size="2" maxlength="2" value="<?=$userinfo->npsize; ?>" onchange="change_npsize()" />

<div id="drag-container" style="position:relative;font-family:<?=$userinfo->font?>;">
    <div id="np_drag" style="color:<?=$userinfo->npcolor?>; font-size:<?=$userinfo->npsize?>px;" class="draggable np_drag" style="position:absolute;left:80px;">
     .::[ NowPlaying SIGnature ]::.
     </div>
</div>

That code is working only in IE. I tried Firefox and Google Chrome.

Share Improve this question asked Feb 19, 2011 at 8:43 CappYCappY 1,5801 gold badge17 silver badges32 bronze badges 1
  • 2 is there any reason you don't you also give the text input an id? – Russ Cam Commented Feb 19, 2011 at 9:00
Add a ment  | 

3 Answers 3

Reset to default 6

The proper usage of getElementsByName() (at least in Firefox) is:

getElementsByName("npsize")[0];

The following works (at least in Chrome):

document.getElementById("np_drag").style.fontSize = document.getElementsByName("npsize")[0].value + "px";

Note the + "px" at the end; you can’t just set it to a numeric value, you need to include the appropriate unit in the value.

I have tried this working code :

your HTML :

<input type="text" name="npsize" size="10" maxlength="2" value="small"/><br/><hr/>
<div id="np_drag">font</div>

your JavaScript :

document.getElementById("np_drag").style.fontSize = document.getElementsByName("npsize")[0].value;

See this working here.

Also, please correct me if I am wrong.

EDIT : I have changed the fiddle with your correctness at other answer.

I think this is not .fontsize ....

it might be .size

发布评论

评论列表(0)

  1. 暂无评论