如何将css类分配给jquery变量?

时间:2017-12-29 11:45:45

标签: jquery html css

大家好我在我的项目中对css和jquery不熟悉我使用基于某些条件循环的不同css类。

if(get_node_details[i]['node_status'] == "ONLINE")
                        {
                            var panel_color = ".panel panel-primary";

                            if(get_node_details[i]['node_type'] == "SECONDARY")
                            {
                                var panel_color = ".panel panel-info";
                            }
                            else if(get_node_details[i]['node_type'] == "STAND_ALONE")
                            {
                                var panel_color = ".panel panel-success";
                            }

我正在为javascript变量分配css类,我正在同一页面中编写动态phtml。

var text = "";
                                text = text + 
                                '<div class="col-xs-12 col-sm-4 col-md-3 col-lg-3">\
                                    <div id="panel_id_'+node_index+'" class='+panel_color+'>\
</div>

但是根据条件,班级不适用于小组....请帮助我

2 个答案:

答案 0 :(得分:0)

您可以将类名称分配给变量,如

var className = "active";
$("#divId").addClass(className);

此处Fiddle链接

答案 1 :(得分:0)

       I have updated your code please check.   
           if(get_node_details[i]['node_status'] == "ONLINE")
                    {
                        var panel_color = ".panel panel-primary";

                        if(get_node_details[i]['node_type'] == "SECONDARY")
                        {
                            panel_color = ".panel panel-info";
                        }
                        else if(get_node_details[i]['node_type'] == "STAND_ALONE")
                        {
                            panel_color = ".panel panel-success";
                        }
                       $("#divId").addClass(panel_color);
                    }