Javascript DOM appendChild 插入图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7932759/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:06:31  来源:igfitidea点击:

DOM appendChild to insert images

javascriptdom

提问by Memochipan

I have this code that creates links.

我有这个创建链接的代码。

   /* Create a link to activate the tab */
    DOM_a = document.createElement("a");
    DOM_a.appendChild(document.createTextNode(t.headingText));
    DOM_a.href = "javascript:void(null);";
    DOM_a.title = t.headingText;
    DOM_a.onclick = this.navClick;

I need to add an image to the link, but when I try to add the image code:

我需要向链接添加图像,但是当我尝试添加图像代码时:

<img src="typo3conf/ext/ori_proyectos/res/images/interes.png">

I get:

我得到:

Link<img src="typo3conf/ext/ori_proyectos/res/images/interes.png">

And not: Link[*_*]

并不是: Link[*_*]

Where [*_*]is the image.

图片在哪里[*_*]

The source code display this:

源代码显示:

&lt;img src="typo3conf/ext/ori_proyectos/res/images/interes.png"&gt;

I don't know how to write it.

我不知道怎么写。

Thanks.

谢谢。

回答by Marcelo Assis

You should create the image using own DOM methods too:

您也应该使用自己的 DOM 方法创建图像:

Something like this:

像这样的东西:

var DOM_img = document.createElement("img");
DOM_img.src = "typo3conf/ext/ori_proyectos/res/images/interes.png";

DOM_a.appendChild(DOM_img);

A working example here.

这里有一个工作示例。