开发者

How to select font with its style attribute value

I have the following html mark up.

<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 sag开发者_开发技巧ittis ac placerat orci varius.</p>

I want to select only the font which has the "background-color:yellow" not any other <font> tag with any style


You can simply use a filter function:

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

UPDATE

To add the class call the addClass function:

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


I tried that , but somehow addClass was not working. so come up with this solution.

$('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'));  
        });
       }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜