gpt4 book ai didi

jquery - 从 style 属性获取 font-size 属性

转载 作者:行者123 更新时间:2023-12-01 07:19:11 24 4
gpt4 key购买 nike

我有一组段落元素

<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- 来设置 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';
});

JS Fiddle demo .

关于jquery - 从 style 属性获取 font-size 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15707836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com