在 jquery 中设置字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3236511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set font size in jquery
提问by KutePHP
I'm not able to change font size using Jquery. I want to change font size of a div. I have defined default font size for body as 12. I tried to change it as follows, but it didn't work :(
我无法使用 Jquery 更改字体大小。我想更改 div 的字体大小。我已将 body 的默认字体大小定义为 12。我尝试按如下方式更改它,但没有用 :(
$("#SelFontSize").change(function(){
$("#"+styleTarget).css({'font-size':'"+$(this).val()+"px'});
});
回答by Anurag
Try:
尝试:
$("#"+styleTarget).css({ 'font-size': $(this).val() });
By putting the value in quotes, it becomes a string, and "+$(this).val()+"px
is definitely not close to a font value. There are a couple of ways of setting the style properties of an element:
通过将值放在引号中,它变成了一个字符串,并且"+$(this).val()+"px
绝对不接近字体值。有几种方法可以设置元素的样式属性:
Using a map:
使用地图:
$("#elem").css({
fontSize: 20
});
Using key and value parameters:
使用键和值参数:
All of these are valid.
所有这些都是有效的。
$("#elem").css("fontSize", 20);
$("#elem").css("fontSize", "20px");
$("#elem").css("font-size", "20");
$("#elem").css("font-size", "20px");
You can replace "fontSize"
with "font-size"
but it will have to be quoted then.
你可以替换为"fontSize"
,"font-size"
但它必须被引用。
回答by husayt
Not saying this is better, just another way:
不是说这更好,只是另一种方式:
$("#elem")[0].style.fontSize="20px";
回答by Bipon Biswas
You can try another way like that:
您可以尝试另一种方式:
<div class="content">
Australia
</div>
jQuery code:
jQuery代码:
$(".content").css({
background: "#d1d1d1",
fontSize: "30px"
})
Now you can add more css property as you want.
现在您可以根据需要添加更多 css 属性。
回答by Sachin R
$("#"+styleTarget).css('font-size', newFontSize);