javascript 如何使用javascript动态地将图像放入锚标记中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5935412/
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 put image in anchor tag dynamically using javascript
提问by Ulhas Tuscano
I have dynamically created image in javascript. I want img to be placed inside an anchor tag how can i do this?
我在javascript中动态创建了图像。我想将 img 放置在锚标签内,我该怎么做?
var element = document.createElement("img");
element = cell.appendChild(element);
element.setAttribute("src", "/Images/Delete.gif");
回答by lawl0r
var anchor=document.createElement('a');
anchor.href='#';
var element = document.createElement("img");
element.setAttribute("src", "/Images/Delete.gif");
anchor.appendChild(element);
cell.appendChild(anchor);
i did not test the code, i used my brain as interpreter, so there may be an error in it. but i think you should get the concept.
我没有测试代码,我用我的大脑作为解释器,所以它可能有错误。但我认为你应该明白这个概念。
回答by Atticus
element.setAttribute before you append it as you are redclaring element to what I presume to now be the cell... or don't assign element = cell.append..
element.setAttribute 在你追加它之前,因为你正在将元素添加到我认为现在是单元格的地方......或者不要分配 element = cell.append..
var element = document.createElement("img");
element.setAttribute("src", "/Images/Delete.gif");
cell.appendChild(element);
What I suggest though would be:
我的建议是:
function insertImage(anchorID, imagesrc){
var img = "<img src='" + imagesrc + "' alt='image' />";
$("#"+anchorID).html($("#"+anchorID).html() + img);
}
I am adding html within html in case there is any text in there you still want to keep.. otherwise negate that second statement of html
我在 html 中添加 html 以防其中有任何文本你仍然想保留..否则否定 html 的第二个语句
You need the jQuery javascript plugin for this to work.. http://docs.jquery.com/Downloading_jQuery
你需要 jQuery javascript 插件才能工作.. http://docs.jquery.com/Downloading_jQuery
If you'd prefer not to include a plugin, then:
如果您不想包含插件,则:
function insertImage(anchorID, imagesrc){
var img = "<img src='" + imagesrc + "' alt='image' />";
document.getElementById(anchorID).innerHtml = img;
}
回答by Rudie
You'll need a reference to the anchor tag (node). Let's say that's a
. Since A
and IMG
are very simple html elements, this will work just fine:
您将需要对锚标记(节点)的引用。让我们说那是a
。由于A
和IMG
是非常简单的 html 元素,因此可以正常工作:
a.innerHTML += '<img src="/Images/Delete.gif">';
Or if you want to replace the content of the A
tag (instead of add the img), just remove the +
:
或者如果你想替换A
标签的内容(而不是添加 img),只需删除+
:
a.innerHTML = '<img src="/Images/Delete.gif">';
Can't fail. In any browser. jQuery is overkill for something this simple (and would do the exact same underneath).
不能失败。在任何浏览器中。jQuery 对于这么简单的事情来说太过分了(并且会在下面做完全相同的事情)。
edit
But it looks you're inserting the img
into a cell
and not an anchor tag... Or your anchor tag is called cell
? In that case, just replace a.
with cell.
=)
编辑
但看起来你是插入img
到 acell
而不是锚标签......或者你的锚标签被称为cell
?在这种情况下,只需替换a.
为cell.
=)