Javascript onclick问题会改变字体大小 [英] Problem with Javascript onclick change font size

查看:85
本文介绍了Javascript onclick问题会改变字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在更改字体大小方面遇到了问题。我尝试过这样的几种方法:



函数resizeText(乘数){

if(document.body.style.fontSize = =){

document.body.style.fontSize =1.0em;

}

document.body.style.fontSize = parseFloat(document.body.style.fontSize)+(乘数* 0.2)+em;

}

但它不起作用,因为我在工作在一个专业人士制作的网站上,他们没有使用em,但价值xx-小,小,大......



这样有用:



函数resizeText(乘数){

if(document.body.style.fontSize ==){



document.body.style.fontSize =small;

}

}



所以我开始做这样的奇怪事情:



函数resizeText(乘数){



var tab = [xx-small,x-small,small,medium,large,x-large,xx-large];



if(document.body.style.fontSize ==){



switch(tab){

case'xx-small' :

document.body.style.fontSize =x-small;

休息;

case'x-small':

document.body.style.fontSize =small;

休息;



等等......



}

}



在我的开关中唯一有用的东西是我对字体大小的默认值。



然后我不认为把一个数组放在一个开关里是个好主意,我一直在考虑循环或使用if / else,但不知何故,我只是找不到答案。我一直做着奇怪的事情。所以,如果有人可以指导我朝着正确的方向前进,我会感激不尽。



现在我已经完成了这个,也没有用:



< script type =text / javascript> //<![CDATA [



函数resizeText (乘数){

var tab = [xx-small,x-small,small,medium,large,x-large,xx-large ];



if(document.body.style.fontSize === tab [0]){



document.body.style.fontSize = tab [1];

}

else if(document.body.style.fontSize === tab [1] )

{

document.body.style.fontSize = tab [2];

}

else if (document.body.style.fontSize === tab [2])

{

document.body.style.fontSize = tab [3];

}

else if(document.body.style.fontSize === tab [3])

{

文件。 body.style.fontSize = tab [4];

}

else if(document.body.style.fontSize === tab [4])

{

document.body.style.fontSize = tab [5];

}

else if(document.body.style.fontSize === tab [5])

{

document.body.style.fontSize = tab [6];

}

}

//< / script>

< button onclick =resizeText(1)> +< / button>

解决方案

你可以使用jQuery:



( body)。css(font-size)



这里有一个例子



< a href =http://codepen.io/anon/pen/WvjNjN> http://codepen.io/anon/pen/WvjNjN [ ^ ]


我自己找到了,但是对于那些试图帮助的人感谢:):



 < script     type   =  text / javascript >   //  < ; [CDATA [ 
var tab = [ xx-small x-small small medium large x-large xx-large];

document.body.style.fontSize = tab [ 0 ];
函数resizeText(乘数){

if (document.body.style.fontSize == tab [ 0 ]){

document.body.style.fontSize = tab [ 1 ];
}
else if (document.body.style.fontSize == tab [ 1 ])
{
document.body.style.fontSize = tab [ 2 ];
}
else if (document.body.style.fontSize == tab [ 2 ])
{
document.body.style.fontSize = tab [ 3 ];
}
else if (document.body.style.fontSize == tab [ 3 ])
{
document.body.style.fontSize = tab [ 4 ];
}
else if (document.body.style.fontSize == tab [ 4 ])
{
document.body.style.fontSize = tab [ 5 ];
}
else if (document.body.style.fontSize == tab [ 5 ])
{
document.body.style.fontSize = tab [ 6 ];
}
}
// ]]>< / script>< ; p>< button onclick =resizeText(1)> +< / button>


Hi everyone, I've got a problem with changing the font-size. I've tried a few methods like this :

function resizeText(multiplier) {
if (document.body.style.fontSize == "") {
document.body.style.fontSize = "1.0em";
}
document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.2) + "em";
}
but it didn't work because I'm working on a website that's been made by professionnals and they didn't use em but the values xx-small, small, large...

This works :

function resizeText(multiplier) {
if (document.body.style.fontSize == "") {

document.body.style.fontSize = "small";
}
}

So I started doing strange stuff like this :

function resizeText(multiplier){

var tab = ["xx-small", "x-small", "small", "medium", "large"," x-large", "xx-large"];

if (document.body.style.fontSize == "") {

switch(tab){
case 'xx-small':
document.body.style.fontSize = "x-small";
break;
case 'x-small':
document.body.style.fontSize = "small";
break;

and so on...

}
}

The only thing that worked in my switch was the default value I put to the font-size.

Then I don't think that putting an array inside a switch is a good idea, I've been thinking about looping or using if/else but somehow I just can't find the answer. I keep doing strange stuff. So if someone could guide me in the right direction, I'dd be gratefull.

Now I've done this, doesn't work either :

<script type="text/javascript">// <![CDATA[

function resizeText(multiplier) {
var tab = ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"];

if (document.body.style.fontSize === tab[0]) {

document.body.style.fontSize = tab[1];
}
else if (document.body.style.fontSize === tab[1])
{
document.body.style.fontSize = tab[2];
}
else if (document.body.style.fontSize === tab[2])
{
document.body.style.fontSize = tab[3];
}
else if (document.body.style.fontSize === tab[3])
{
document.body.style.fontSize = tab[4];
}
else if (document.body.style.fontSize === tab[4])
{
document.body.style.fontSize = tab[5];
}
else if (document.body.style.fontSize === tab[5])
{
document.body.style.fontSize = tab[6];
}
}
// </script>

<button onclick="resizeText(1)" >+</button>

解决方案

you can use jQuery:


("body").css("font-size")

here is an example

http://codepen.io/anon/pen/WvjNjN[^]


I've found it myself but thanks anyway for those who tried to help :) :

<script type="text/javascript">// <![CDATA[
var tab = ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"];

document.body.style.fontSize = tab[0];
function resizeText(multiplier) {

 if (document.body.style.fontSize == tab[0]) {

document.body.style.fontSize = tab[1];
}
else if (document.body.style.fontSize == tab[1])
{
document.body.style.fontSize = tab[2];
}
else if (document.body.style.fontSize == tab[2])
{
document.body.style.fontSize = tab[3];
}
else if (document.body.style.fontSize == tab[3])
{
document.body.style.fontSize = tab[4];
}
else if (document.body.style.fontSize == tab[4])
{
document.body.style.fontSize = tab[5];
}
else if (document.body.style.fontSize == tab[5])
{
document.body.style.fontSize = tab[6];
}
}
// ]]></script><p><button onclick="resizeText(1)">+</button> 


这篇关于Javascript onclick问题会改变字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆