我对 Google Closure 库比较陌生,我目前的障碍是获得一个按钮以编程方式呈现图像(而不是文本)。我尝试添加一个 img 标签作为按钮的内容:
var image = goog.dom.createDom('img', { 'src' : 'foo.png' });
var button = new goog.ui.Button(image);
但是,尽管页面中确实呈现了一个空白按钮,但没有出现图像。检查生成的 HTML 会显示一个没有内容的按钮标记。
我还考虑过添加一个样式属性来指定background-image
. 但是,我不清楚如何通过 Button API 完成此操作。
关于如何做到这一点的任何想法或例子?感谢帮助。
编辑:这是我在 Dave Paroulek 的帮助下得出的解决方案:
var button = new goog.ui.CustomButton();
button.render(goog.dom.getElement('button-row'));
var style = button.getElement().style;
style.backgroundImage = 'url(foo.png)';
style.backgroundPosition = 'center center';
style.backgroundRepeat = 'no-repeat';