jquery match elements with style font-size in px

2019-03-05 03:08发布

How to target element with inline-style font-size defined in pixels (ex: font-size:12px) in order to modify font-size on a button click.

Can someone help finish this javascript code :

html

<div class='parent' style='font-size:15px'>
    <div>div 1 no inline styling</div>
    <div style='font-size:1em'>div 2 inlined font-size 1em</div>
    <div class='div-3'>div 3, CSS font-size:14px;</div>
    <div style='font-size:22px'>div 4 inlined font-size 22px</div>
    <div style='font-size:16pt'>div 5 inlined font-size 16pt</div>
    <div style='font-size:80%'>div 6 inlined font-size 80%</div>
</div>
<div id="output_box"></div>

javascript

$('*').filter(function() {
     /*var a-match-in-px-units = some-regular-expression-someelse-can-help-about;*/
     return $(this).css('font-size') == '11';
    $('#output_box').html($(this));
});

$("#trigger").click(function() {
    alert('to initialize font-size in px to bigger/lesser unit function()');    
   /* This is to apply font-resizing */     
});

I prepared a jsFiddle

I found this other answer useful Find elements which have greater font-size than specified

2条回答
SAY GOODBYE
2楼-- · 2019-03-05 03:25

You may try indexOf on style attribute:

$('.parent').each(function (index, value) {
    if (/font-size:[^;]+px/.test($(this).attr('style'))) {
        alert(index + ": " + $(this).attr('style'));
    }
});

Here is Demo Fiddle

查看更多
虎瘦雄心在
3楼-- · 2019-03-05 03:38

JQuery .css('font-size') always returns pixels, even if the original size was defined with a different unit. I would use .attr('style') instead (works with inline styles only) :

var result = $('*').filter(function () {
    var style = $(this).attr('style');
    return /font-size:[^;]+px/.test(style);
});

Here is a demo : http://jsfiddle.net/wared/8LcD9/.

查看更多
登录 后发表回答