英文:
GWT: How to update an image specified in html?
问题
以下是翻译好的部分:
我有以下的HTML字符串添加到了一个HtmlPanel中。如何修改图片?
private static String htmlStr1 = "<div class=\"container\"><img id=\"image\" src=\"../image/default.jpg\" alt=\"Avatar\" class=\"image\">" +
"<div class=\"overlay\">" +
"<a href=\"#\" class=\"icon\" title=\"User Profile\">" +
"<i class=\"fa-user\">" +
"</i>" +
"</a>" +
"</div>" +
"</div>";
我想通过以下代码加载另一张图片,但是失败了。我已经从Firefox中检查了HTML,甚至原始的htmlStr1
也不在那里。在没有o.setImage
的情况下,htmlStr1
可以正确加载。
Overlay o = new Overlay();
// 在没有 o.setImage 的情况下,html字符串可以正确加载
o.setImage("../image/default1.jpg");
RootPanel.get("htmlStrContainer").add(o);
代码如下:
public class Overlay extends HTMLPanel {
private static String htmlStr1 = "<div class=\"container\"><img id=\"image\" src=\"../image/default.jpg\" alt=\"Avatar\" class=\"image\">" +
"<div class=\"overlay\">" +
"<a href=\"#\" class=\"icon\" title=\"User Profile\">" +
"<i class=\"fa-user\">" +
"</i>" +
"</a>" +
"</div>" +
"</div>";
private String imgUrl = "";
public Overlay() {
super(htmlStr1);
}
// 通过这个函数更新图片
public void setImage(String _imgUrl) {
this.imgUrl = _imgUrl;
Image img = Image.wrap(Document.get().getElementById("image"));
img.setUrl(_imgUrl);
}
}
如果我有两个Overlay
要添加:
Overlay o1 = new Overlay();
Overlay o2 = new Overlay();
RootPanel.get("htmlStrContainer").add(o1);
RootPanel.get("htmlStrContainer").add(o2);
o1.setImage("../image/default1.jpg");
o2.setImage("../image/default2.jpg");
那么为什么只有第一个Overlay
正确初始化了图片?
在HTMLPanel附加之前修改HTMLPanel的内容有什么好的方法吗?
谢谢。
英文:
I have the following html string added to a HtmlPanel. How can I modify the image?
private static String htmlStr1="<div class=\"container\"><img id=\"image\" src=\"../image/default.jpg\" alt=\"Avatar\" class=\"image\">" +
"<div class=\"overlay\">" +
"<a href=\"#\" class=\"icon\" title=\"User Profile\">" +
"<i class=\"fa-user\">" +
"</i>" +
"</a>" +
"</div>" +
"</div>";
I would like to load another image by the following code, but it fails. I have checked the html from Firefox, even the original htmlStr1 is not there. Without o.setImage, the htmlStr1 is loaded correctly.
Overlay o=new Overlay();
//Without o.setImage, the html string is loaded correctly
o.setImage("../image/default1.jpg");
RootPanel.get("htmlStrContainer").add(o);
The code is as the following
public class Overlay extends HTMLPanel{
private static String htmlStr1="<div class=\"container\"><img id=\"image\" src=\"../image/default.jpg\" alt=\"Avatar\" class=\"image\">" +
"<div class=\"overlay\">" +
"<a href=\"#\" class=\"icon\" title=\"User Profile\">" +
"<i class=\"fa-user\">" +
"</i>" +
"</a>" +
"</div>" +
"</div>";
private String imgUrl="";
public Overlay()
{
super(htmlStr1);
}
//update the image by this function
public void setImage(String _imgUrl)
{
this.imgUrl=_imgUrl;
Image img=Image.wrap(Document.get().getElementById("image"));
img.setUrl(_imgUrl);
}
}
If I have a 2 Overlay to add,
Overlay o1=new Overlay();
Overlay o2=new Overlay();
RootPanel.get("htmlStrContainer").add(o1);
RootPanel.get("htmlStrContainer").add(o2);
o1.setImage("../image/default1.jpg");
o2.setImage("../image/default2.jpg");
Then, why only the first Overlay is initiated with the image correctly?
What is the best to modify the content of the HTMLPanel? Is it possible to modify the content of the HTMLPanel before it is attached?
Thanks
答案1
得分: 0
document.getElementById
方法在元素未附加到文档时不起作用!首先,要么将HTML分配给文档的某个部分,要么创建一个新元素仅用于该片段,将HTML分配为该新元素的内容。然后,您可以查看该元素的内容,找到所需的图像,并进行微调,直到达到所需效果。
英文:
The document.getElementById
method does not work if the element isn't attached to the document! First, either assign the HTML to some part of the document, or make a new element just for that snippet, and assign the HTML as the contents of that new element. Then, you can poke into the contents of that element and find the desired image, and tweak it until it is right.
答案2
得分: 0
尝试一下这段代码。HTMLPanel
有它自己的方法 getElementById
,即使在 HTMLPanel
未附加到文档时也能正常工作。
public class Overlay extends HTMLPanel {
private static final ContainerTemplate TEMPLATE = GWT.create(ContainerTemplate.class);
private final String imgElementId;
public Overlay(String imgElementId) {
super(TEMPLATE.template(imgElementId));
this.imgElementId = imgElementId;
}
public static Overlay newInstance() {
return new Overlay(createUniqueId());
}
public void setImageUrl(String imgUrl) {
getElementById(imgElementId).<ImageElement>cast().setSrc(imgUrl);
}
interface ContainerTemplate extends SafeHtmlTemplates {
@Template(
"<div class=\"container\"><img id=\"{0}\" src=\"../image/default.jpg\" alt=\"Avatar\" " +
"class=\"image\">" +
"<div class=\"overlay\">" +
"<a href=\"#\" class=\"icon\" title=\"User Profile\">" +
"<i class=\"fa-user\"></i>" +
"</a>" +
"</div>" +
"</div>")
SafeHtml template(String imgId);
}
}
英文:
Try this code. HTMLPanel
has its own method getElementById
which works even when HTMLPanel
is not attached to the document.
public class Overlay extends HTMLPanel {
private static final ContainerTemplate TEMPLATE = GWT.create(ContainerTemplate.class);
private final String imgElementId;
public Overlay(String imgElementId) {
super(TEMPLATE.template(imgElementId));
this.imgElementId = imgElementId;
}
public static Overlay newInstance() {
return new Overlay(createUniqueId());
}
public void setImageUrl(String imgUrl) {
getElementById(imgElementId).<ImageElement>cast().setSrc(imgUrl);
}
interface ContainerTemplate extends SafeHtmlTemplates {
@Template(
"<div class=\"container\"><img id=\"{0}\" src=\"../image/default.jpg\" alt=\"Avatar\" " +
"class=\"image\">" +
"<div class=\"overlay\">" +
"<a href=\"#\" class=\"icon\" title=\"User Profile\">" +
"<i class=\"fa-user\">" +
"</i>" +
"</a>" +
"</div>" +
"</div>")
SafeHtml template(String imgId);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论