我有一组段落元素
<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';
});
});
第二个替代方案大致相同,尽管它直接使用内联 css()
方法通过查找字符串 font- 来设置
和后面的数字,然后删除 font-size
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';
});
关于jquery - 从 style 属性获取 font-size 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15707836/