如何在script标记中设置类或样式

How to set class or style in script tag?

本文关键字:设置 样式 script      更新时间:2023-09-26

如何在脚本标记中设置类或样式?

我试着做这个买不起作用的

<script>
    $(function() {
        $('.J_countdown2').countdown({
            tmpl : '<span style='font-size: 50px; '>%{d}</span>days'
        });
    });
</script>

i填充style='font-size: 50px; '

您可以放置双引号或转义单引号:

$(function() {
    $('.J_countdown2').countdown({
        tmpl : '<span style="font-size: 50px; ">%{d}</span>days'
    });
});

或用':转义内部单引号

tmpl : '<span style=''font-size: 50px; ''>%{d}</span>days' 

甚至你也可以试试这个:

$('.J_countdown2').countdown({
    tmpl : $('<span/>', {
              class: "font50", // <----set class name
              style : "font-size:50px", // <---style if you want against class
              text : '%{d}days' //<-----put the text like this
           })
});

其中font50是类名:

.font50{
   font-size: 50px;
}

由于您已经在使用jQuery,最好的方法是将.addClass("className"(添加到元素中,并在外部CSS文件中设置所需的所有样式…:-(