在Javascript中将图像插入表格单元格 [英] Insert image into table cell in Javascript
本文介绍了在Javascript中将图像插入表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将图像插入刚创建的新单元格。我该怎么做?任何人都可以指导我做这件事吗?
这是我的代码插入单元格:
<!DOCTYPE html>
< html>
< head>
< script>
函数displayResult()
{
var firstRow = document.getElementById(myTable)。rows [0];
var x = firstRow.insertCell(-1);
x.innerHTML =新单元格
}
< / script>
< / head>
< body>
< table id =myTableborder =1>
< tr>
< td>第一个单元格< / td>
< td>第二个单元格< / td>
< td>第三个单元格< / td>
< / tr>
< / table>
< br>
< button type =buttononclick =displayResult()>插入单元格< / button>
< / body>
< / html>
我想要的是在创建的单元格中插入图片。
<您可以创建图像元素并将其添加到新单元格中:
<$ c $ c> function displayResult()
{
var firstRow = document.getElementById(myTable)。rows [0];
var x = firstRow.insertCell(-1);
x.innerHTML =新单元格;
var img = document.createElement('img');
img.src =link to image here;
x.appendChild(img);
注意为图像构建原始HTML,如果您这样做你需要确保你转义 src
和其他任何属性。
I want to insert images to the new cell just created. How can I do it? Can anyone guide me in doing it? Here's my code to insertcells:
<!DOCTYPE html>
<html>
<head>
<script>
function displayResult()
{
var firstRow=document.getElementById("myTable").rows[0];
var x=firstRow.insertCell(-1);
x.innerHTML="New cell"
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>First cell</td>
<td>Second cell</td>
<td>Third cell</td>
</tr>
</table>
<br>
<button type="button" onclick="displayResult()">Insert cell</button>
</body>
</html>
All I want is to insert image in the cell created.
解决方案
You can create the image element and append it to the new cell:
function displayResult()
{
var firstRow=document.getElementById("myTable").rows[0];
var x=firstRow.insertCell(-1);
x.innerHTML="New cell";
var img = document.createElement('img');
img.src = "link to image here";
x.appendChild(img);
}
Beware of building the raw HTML for the image, if you do it that way you'll need to make sure that you escape the src
and any other attributes.
这篇关于在Javascript中将图像插入表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文