jquery match elements with style font-size in px

给你一囗甜甜゛ 提交于 2019-12-02 15:19:12

问题


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


回答1:


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/.




回答2:


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



来源:https://stackoverflow.com/questions/20592932/jquery-match-elements-with-style-font-size-in-px

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!