问题描述
我有一组段落元素
<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
.我无法使用当前代码
I have to write px
after font size degree. I am unable to use my current code
loop
$(this).css("font-size")
我不能使用它,因为它显示了父级div
I can't use this because it shows me the font size of the parent div
推荐答案
有两种显而易见的方法,第一种方法是查看style
属性,以搜索font-size:
字符串后的数字,然后将该字符串替换为其自身.在其中添加px
There are two obvious approaches, the first looks at the style
attribute searching for the numbers following the font-size:
string and replaces that string with itself, though adding px
to it:
$('p').attr('style',function(i,s){
return s.replace(/font-size:\s*(\d+.{0,1}\d*)/, function(a){
return a + 'px';
});
});
第二种方法非常相似,尽管它直接使用内联css()
方法通过找到字符串font-size:
和以下数字,然后除去font-size:
字符串来设置font-size
(因此设置将font-size
属性添加到该字符串后面的数字,并在其中添加"px":
The second alternative is much the same, albeit it directly uses the inline css()
method to set the font-size
by finding the string font-size:
, and the following numbers, then removing the font-size:
string (so setting the font-size
property to the numbers following that string and adding 'px' to it:
$('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';
});
这篇关于从样式属性获取字体大小属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!