1

早晨,

当我从数据库返回数据以显示更改/更新时,我正在设置 td 的类。在返回的字段之一上,我有 4 个选项,我需要根据结果更改该字段的 css 类。

 "<td" + (res.prodPublished ? "" : " class='updated'") + ">" + res.prodPublished + "</td>" 

我目前正在使用上述内容,如果您只有真假,那也没关系。但是,我有... True,False,Processing,New。

根据这些选项,我如何更改课程?

提前致谢。

4

3 回答 3

2

With a js function?

function get_cell_class(type) {
    if(type == "True") {
        return " class='updated'";
    }

    if(type == "False") {
        return " class='class_2'";
    }

    ..
}

Then within your HTML code:

"<td" + get_cell_class(res.prodPublished) + ">" + res.prodPublished + "</td>"

Method 2:

Or you can just make CSS classes of the values:

.my_css_true {
    color: blue;
}

.my_css_updated {
    color: orange;
}

Then in HTML:

"<td class=\"my_css_" + res.prodPublished + "\">" + res.prodPublished + "</td>"

Hope this helps!

于 2012-07-11T09:05:03.303 回答
2

使用字典

var classes = {
  'True': ' class="updated"',
  'False': '',
  'New': ' class="other-classname-new"',
  'Processing': ' class="other-classname-processing"'
};

//some time later

 "<td" + classes[res.prodPublished] + ">" + res.prodPublished + "</td>"

请注意,"True"and"False"是字符串,而不是布尔原语。因此res.prodPublished也需要是一个字符串。

于 2012-07-11T09:06:33.897 回答
0

You just need to setup an if else instead

You can write it as a standard if else or the same way you do now.

var theClass = (result == "true" ? "class for true": 
      (result == "false" ? "class for false":
              (result == "processing" ? "class for processing": "class for new")));
于 2012-07-11T09:05:18.577 回答