2011-09-23 47 views
1

的我有以下代碼:顏色阿帕奇檢票口組件

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" 
    xml:lang="en" lang="en"> 
    <body> 
    <wicket:panel> 
     <div wicket:id="serviceListContainer"> 
     <table> 
      <tr wicket:id="serviceListView"> 
      <td> 
       <span wicket:id="service.name"></span> 
      </td> 
      <td> 
       <span wicket:id="service.state"></span> <!-- I WANT TO COLOR THIS PART IN RED! --> 
      </td> 
      </tr> 
     </table> 
     </div> 
    </wicket:panel> 
    </body> 
</html> 

我如何可以改變輸出文本,它將取代「service.state」的顏色?我嘗試了<font color="red">,但沒有任何區別。 謝謝!

回答

8

其他的答案表明如何在HTML模板中添加style="..."屬性。如果,另一方面,你不希望靜態地做到這一點(比如,你需要計算的顏色,然後將其添加到組件),你應該addAttributeModifier到組件。

例(未經測試):

Label l = new Label("service.state", ...); 
IModel<String> colorModel = new AbstractReadOnlyModel<String>() { 
    public String getObject() { 
     return "color: red;"; // Dummy example 
    } 
}; // Some model, holding a string representation of a CSS style attribute (e.g., "color: ...;") based on some date and/or calculation 
l.add(new AttributeModifier("style", true, colorModel); 

你甚至可以使用SimpleAttributeModifier如果你不需要基於拉模型:

Label l = new Label("service.state", ...); 
String styleAttr = "color: red;"; 
l.add(new SimpleAttributeModifier("style", styleAttr)); 

1)假設setRenderBodyOnly(true)已有而不是已被ca LLED。這將從輸出中移除包裝<span>元素。

+0

AttributeModifier似乎被棄用(鏈路是404),並且SimpleAttributeModifier也是404鏈接。 –

+0

是的,很好。當時Wicket 1.4是當前的:) – jensgram

0
<span wicket:id="service.state" style="color:red"></span> 

或更好的是使用適當的CSS類

3

W3Schools

的<字體>標籤以HTML 4過時,從HTML5除去。
萬維網聯盟(W3C)已將其建議從 中移除。在HTML 4中,應該使用樣式表(CSS) 來定義許多HTML元素的佈局和顯示屬性。

使用的風格,哪怕是內聯樣式,而不是:

<span style="color:red">this is red text</span> 

如果你使用的檢票你要確保你不是在用成分與setRenderBodyOnly(true) ID service.state ,因爲這會將樣式剝離<span>標籤。