java 如何从 Wicket 组件中删除 CSS 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10135791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove a CSS class from a Wicket component?
提问by Jonik
It's pretty straightforward to dynamically adda CSS class to a component in Java code using an AttributeAppender:
使用AttributeAppender将CSS 类动态添加到 Java 代码中的组件非常简单:
component.add(new AttributeAppender("class", true, new Model<String>("foo"), " "));
Or, if you've extracted the above into an appropriate utility method or class, simply something like:
或者,如果您已将上述内容提取到适当的实用程序方法或类中,则只需类似以下内容:
component.add(WicketUtils.cssClassAppender("foo"));
But how can I remove a CSS class?
但是我怎样才能删除一个 CSS 类呢?
You can easily remove allCSS classes by clearing the class attribute altogether:
您可以通过完全清除 class 属性轻松删除所有CSS 类:
component.add(new SimpleAttributeModifier("class", ""));
...but that is not acceptable if the component has other CSS classes that you do notwish to remove.
...但如果组件其它CSS类,你这样做是不能接受不希望删除。
Wicket 1.4 (but feel free to post advice specific to later versions too).
Wicket 1.4(但也可以随意发布特定于更高版本的建议)。
回答by Jonik
Here's one way I came up with:
这是我想出的一种方法:
public class CssClassRemover extends AttributeModifier {
public CssClassRemover(String cssClass) {
super("class", false, new Model<String>(cssClass));
}
@Override
protected String newValue(String currentValue, String valueToRemove) {
// NB: naive approach; breaks with e.g. "foo foo-bar" & "foo"
return currentValue.replaceAll(valueToRemove, "");
}
}
The code that uses the above helper would then be:
使用上述帮助程序的代码将是:
component.add(new CssClassRemover("foo"))
(Of course you can also just create anonymous AttributeModifier subclasses as needed, but putting the logic in a separate utility class or method cleans it up a lot.)
(当然,您也可以根据需要创建匿名的 AttributeModifier 子类,但是将逻辑放在单独的实用程序类或方法中会清理很多。)
Edit: An improved version of newValue()
that handles corner cases better (see comment by biziclop). NB: uses Guava. (You're welcome to post simpler (regex?) versions.)
编辑:改进版本newValue()
可以更好地处理极端情况(请参阅 biziclop 的评论)。注意:使用番石榴。(欢迎您发布更简单的(正则表达式?)版本。)
@Override
protected String newValue(String currentValue, String valueToRemove) {
if (currentValue == null) return "";
Set<String> classes = Sets.newHashSet(Splitter.on(" ").split(currentValue));
classes.remove(valueToRemove);
return Joiner.on(" ").join(classes);
}
回答by nschum
Building on Jonik's answer, the following adds negative lookahead to ignore occurrences in the middle of a different style class (and is case insensitive).
基于Jonik 的回答,以下内容添加了否定前瞻以忽略不同样式类中间的事件(并且不区分大小写)。
public class StyleClassRemover extends AttributeModifier {
public StyleClassRemover(final String cssClass) {
super("class", false, Model.of(cssClass));
}
@Override
protected String newValue(final String currentValue, final String valueToRemove) {
if (currentValue == null) {
return "";
}
final String patternString = "(^|\s+)" + Pattern.quote(valueToRemove) + "(?!\S)";
return Pattern.compile(patternString, Pattern.CASE_INSENSITIVE).matcher(currentValue).replaceAll("");
}
}
Tested input: http://fiddle.re/ah0ca6
测试输入:http: //fiddle.re/ah0ca6
回答by martin-g
Wicket 1.5+ has org.apache.wicket.AttributeModifier#remove()
Wicket 1.5+ 有 org.apache.wicket.AttributeModifier#remove()