GWT:如何更新在HTML中指定的图像?

huangapple go评论68阅读模式
英文:

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=&quot;&lt;div class=\&quot;container\&quot;&gt;&lt;img id=\&quot;image\&quot; src=\&quot;../image/default.jpg\&quot; alt=\&quot;Avatar\&quot; class=\&quot;image\&quot;&gt;&quot; +
							   &quot;&lt;div class=\&quot;overlay\&quot;&gt;&quot; +
							   		&quot;&lt;a href=\&quot;#\&quot; class=\&quot;icon\&quot; title=\&quot;User Profile\&quot;&gt;&quot; +
							   			&quot;&lt;i class=\&quot;fa-user\&quot;&gt;&quot; +
							   			&quot;&lt;/i&gt;&quot; +
							   		&quot;&lt;/a&gt;&quot; +
							   	&quot;&lt;/div&gt;&quot; +
							   	&quot;&lt;/div&gt;&quot;;

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(&quot;../image/default1.jpg&quot;);
	RootPanel.get(&quot;htmlStrContainer&quot;).add(o);

The code is as the following

public class Overlay extends HTMLPanel{
private static String htmlStr1=&quot;&lt;div class=\&quot;container\&quot;&gt;&lt;img id=\&quot;image\&quot; src=\&quot;../image/default.jpg\&quot; alt=\&quot;Avatar\&quot; class=\&quot;image\&quot;&gt;&quot; +
							   &quot;&lt;div class=\&quot;overlay\&quot;&gt;&quot; +
							   		&quot;&lt;a href=\&quot;#\&quot; class=\&quot;icon\&quot; title=\&quot;User Profile\&quot;&gt;&quot; +
							   			&quot;&lt;i class=\&quot;fa-user\&quot;&gt;&quot; +
							   			&quot;&lt;/i&gt;&quot; +
							   		&quot;&lt;/a&gt;&quot; +
							   	&quot;&lt;/div&gt;&quot; +
							   	&quot;&lt;/div&gt;&quot;;
private String imgUrl=&quot;&quot;;
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(&quot;image&quot;));
	img.setUrl(_imgUrl);
}
}

If I have a 2 Overlay to add,

Overlay o1=new Overlay();
Overlay o2=new Overlay();
RootPanel.get(&quot;htmlStrContainer&quot;).add(o1);
RootPanel.get(&quot;htmlStrContainer&quot;).add(o2);
o1.setImage(&quot;../image/default1.jpg&quot;);
o2.setImage(&quot;../image/default2.jpg&quot;);

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).&lt;ImageElement&gt;cast().setSrc(imgUrl);
    }

    interface ContainerTemplate extends SafeHtmlTemplates {
        @Template(
                &quot;&lt;div class=\&quot;container\&quot;&gt;&lt;img id=\&quot;{0}\&quot; src=\&quot;../image/default.jpg\&quot; alt=\&quot;Avatar\&quot; &quot; +
                        &quot;class=\&quot;image\&quot;&gt;&quot; +
                        &quot;&lt;div class=\&quot;overlay\&quot;&gt;&quot; +
                        &quot;&lt;a href=\&quot;#\&quot; class=\&quot;icon\&quot; title=\&quot;User Profile\&quot;&gt;&quot; +
                        &quot;&lt;i class=\&quot;fa-user\&quot;&gt;&quot; +
                        &quot;&lt;/i&gt;&quot; +
                        &quot;&lt;/a&gt;&quot; +
                        &quot;&lt;/div&gt;&quot; +
                        &quot;&lt;/div&gt;&quot;)
        SafeHtml template(String imgId);
    }

}

huangapple
  • 本文由 发表于 2020年7月24日 17:07:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63070424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定