在css行中插入值

时间:2015-08-20 13:33:19

标签: jquery css

我正在尝试将padding-top值添加到我在标头中附加的css中,但只有color正在运行:

        var PaddingTOp = $('#PaddingTOp').val();
        var PaddingRight = $('#PaddingRight').val();
        var PaddingBottom = $('#PaddingBottom').val();
        var PaddingLeft = $('#PaddingLeft').val();

        //Class Name
        $("<style type='text/css'> .redbold{ color:#f00; padding-top: PaddingTOp ;} </style>").appendTo("head");
        $(".container").addClass("redbold");

2 个答案:

答案 0 :(得分:2)

$("<style type='text/css'> .redbold{ color:#f00; padding-top: "+PaddingTOp+" ;} </style>").appendTo("head");

答案 1 :(得分:0)

    var PaddingTOp = $('#PaddingTOp').val();
    var PaddingRight = $('#PaddingRight').val();
    var PaddingBottom = $('#PaddingBottom').val();
    var PaddingLeft = $('#PaddingLeft').val();

    //Class Name

    var cssToApply = "color:#f00;";
    $.trim(PaddingTOp).length > 0 ?  cssToApply + "padding-top:"+PaddingTOp +";" : "";
    $.trim(PaddingRight ).length > 0 ?  cssToApply + "padding-right:"+PaddingRight +";" : "";
    $.trim(PaddingBottom ).length > 0 ?  cssToApply + "padding-bottom:"+PaddingBottom +";" : "";
    $.trim(PaddingLeft ).length > 0 ?  cssToApply + "padding-left:"+PaddingLeft +";" : "";

    $("<style type='text/css'> .redbold{ "+ cssToApply +"} </style>").appendTo("head");
    $(".container").addClass("redbold");