的字体大小
我有一组段落元素
<p style="font-family:Tahoma; font-size:10.666666666666666; margin:0 0 0 0;">
<p style="font-family:Tahoma; font-size:10.666666666666666; margin:0 0 0 0;">
<p style="font-family:Tahoma; font-size:10.666666666666666; margin:0 0 0 0;">
<p style="font-family:Tahoma; font-size:10.666666666666666; margin:0 0 0 0;">
我必须在字体大小之后写px
。我无法使用我当前的代码
loop
$(this).css("font-size")
我不能用这个因为它显示了父元素div
有两种明显的方法,第一种方法是查看style
属性,搜索font-size:
字符串后面的数字,并用自身替换该字符串,尽管将px
添加到其中:
$('p').attr('style',function(i,s){
return s.replace(/font-size:s*(d+.{0,1}d*)/, function(a){
return a + 'px';
});
});
JS Fiddle demo.
第二种选择非常相似,尽管它直接使用内联css()
方法通过查找字符串font-size:
和以下数字来设置font-size
,然后删除font-size:
字符串(因此将font-size
属性设置为该字符串后面的数字并添加'px'):
$('p').css('font-size',function(){
var s = $(this).attr('style').match(/(?:font-size:)s*(d+.{0,1}d*)/)[0].replace(/font-size:/,'');
return s + 'px';
});
JS提琴演示
这将读取当前值,在其后面加上"px",并重新赋值。
$(parent).children('p').each(function(){
$(this).css("font-size", $(this).css("font-size") + "px");
});