Jquery Changing CSS class of object based on numeric content

前端 未结 3 403
粉色の甜心
粉色の甜心 2021-01-25 14:20

Going to be outputting percentages dynamically 0-100%

Want to add CSS class based on percentage. Red for 0% and Blue for 100% progressively.

Markup would be

3条回答
  •  有刺的猬
    2021-01-25 14:33

    I see nothing wrong with using :contains() (see http://jsfiddle.net/b3tUf/), but if you want to change your code, you could always just use conditional statements.

    if( $("span").text() == "0%" ) {
        $("span").css("color", "#0000ff");
    } elseif ( $("span").text() == "100%" ) {
        ...
    }
    

    or for classes:

    if( $("span").text() == "0%" ) {
        $("span").removeClass().addClass("red");
    } elseif ( $("span").text() == "100%" ) {
        $("span").removeClass().addClass("blue");
    }
    

提交回复
热议问题