[英]Displaying an image dynamically in a html table in JavaScript
I am trying to display an image in a table.我正在尝试在表格中显示图像。 This is what I have tried:
这是我尝试过的:
imgv = "/img/koala.jpg";
dcontent = "<table style='background-color:purple;'><tr><td style='width: 100px; color: red;'> Date </td>";
dcontent += "<tr><td style='width: 100px;'><img src="' + imgv + '"/></td>";
I am getting a compile time error in the second line:我在第二行收到编译时错误:
"; expected".
“; 预期的”。
I tried many syntax like:我尝试了许多语法,例如:
<img src=" + imgv + "/></td>
but nothing is getting compiled.但没有任何东西被编译。
Correct answer is replacing "' + imgv + '"
with '" + imgv + "'
正确答案是用
'" + imgv + "'
替换"' + imgv + '"
imgv = "/img/koala.jpg";
dcontent = '<table style="background-color:purple;"><tr><td style="width: 100px; color: red;"> Date </td>';
dcontent += "<tr><td style='width: 100px;'><img src='" + imgv + "'/></td>";
You need to replace "' + imgv + '"
with '" + imgv + "'
, because ' is stronger than ". For example, when you create variable named foo:您需要将
"' + imgv + '"
替换为'" + imgv + "'
,因为 ' 比 " 强。例如,当您创建名为 foo 的变量时:
var foo = "write sentence 'like' that";
in html you will get just "write sentence that".在 html 中,您只会得到“写句子”。
SOLUTION is to replace them, then解决方案是替换它们,然后
var foo = 'write sentence "like" that'
will output exactly write sentence "like" that - nothing wrong :-)将准确地输出写“喜欢”的句子-没有错:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.