在Wicket中,您可以轻松访问或操作HTML标签。 假设您有一个HTML文本框组件,并由div标签包裹,并且如果文本框验证失败,则div标签应以错误颜色突出显示。
在上述情况下,您可以实现“ AbstractBehavior ”类,以将属性动态添加到HTML标签。 请参阅以下示例,
原始HTML
Hello ~ Wicket leaning curve is high, do you?
用Wicket修改AbstractBehavior
WebMarkupContainerWithAssociatedMarkup divtest =
new WebMarkupContainerWithAssociatedMarkup("wicket_id_test");
//validation failed , add AbstractBehavior to the div test container
divtest.add(new AbstractBehavior() {
public void onComponentTag(Component component, ComponentTag tag) {
tag.put("style", "background-color:red");
}
});
结果是这样的:
Hello ~ this is testing for adding attribute into above tag in Wicket ~
标签: 检票口
翻译自: https://mkyong.com/wicket/how-to-dynamic-add-attribute-to-a-html-tag-in-wicket/