【问题标题】:How to select font with its style attribute value如何选择具有样式属性值的字体
【发布时间】:2010-05-24 22:42:23
【问题描述】:

我有以下 html 标记。

<p> lorem ipsum lorem ipsum 
<font style="background-color:yellow"> ipsum </font> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam id enim in tellus sollicitudin viverra. Morbi nec ipsum ligula, non volutpat enim. In quis metus <font style="color:red"> Tincidunt lorem </font>blandit faucibus. Nam condimentum facilisis vestibulum. Nunc tristique est vel erat sagittis ac placerat orci varius.</p>

我只想选择带有“background-color:yellow”的字体,而不是任何其他带有任何样式的&lt;font&gt;标签

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您可以简单地使用过滤功能:

    $("font").filter(function(){
        var bg=$(this).css("background-color");
        return bg=="yellow" || bg=="rgb(255,255,0)";
    });
    

    更新

    添加类调用addClass函数:

    $("font").filter(function(){
        var bg=$(this).css("background-color");
        return bg=="yellow" || bg=="rgb(255,255,0)";
    }).addClass("hlight");
    

    【讨论】:

    • 好的,我过滤了,现在如何将 Class 添加到该 FONT 标签中,如 wise 'hlight'。
    • 更新了答案希望对你有帮助
    • 非常感谢..请查看我的答案以获取我使用的代码。
    【解决方案2】:

    我试过了,但不知何故 addClass 不起作用。所以想出这个解决方案。

    $('font').filter(function(){ 
                var bg=$(this).css("background-color"); 
                alert('bghlight - ' + bg);
           if ( bg == "rgb(255, 255, 0)" || bg == "yellow" )
           {
            $(this).addClass('hlight');
            $('font.hlight').each(function(){
            $(this).replaceWith($('<abbr>' + this.innerHTML + '</abbr>').addClass('abbr-hlight'));  
            });
           }
        });
    

    【讨论】:

      猜你喜欢
      • 2015-02-09
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 2013-07-20
      • 2015-12-28
      • 1970-01-01
      • 2018-08-09
      相关资源
      最近更新 更多