将变量插入innerHTML

时间:2016-12-08 01:41:07

标签: javascript function variables html-table

我试图创建一个出现在id =" here"。

的表格

问题是我需要把" var i"表格标题内(th)。

执行时表不会出现 - 我认为这是因为当我添加" var i"时,字符串会中断。

有什么方法可以解决这个问题吗?是否有一种已知的方法将变量插入到这样的表中?

感谢任何/所有帮助:)

<p id="here"></p>
<button type="button" onclick="test();"></button>

<script>

var i = Dogs;
function test() {document.getElementById("here").innerHTML = 
"<table><tr><th>" + i + "</th></tr></table>";}

</script>

2 个答案:

答案 0 :(得分:2)

向狗添加引号。字符串文字必须有引号。

@SpringBootApplication
public class ProductServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }
}
var i = "Dogs";
function test() {document.getElementById("here").innerHTML = 
"<table><tr><th>" + i + "</th></tr></table>";}

答案 1 :(得分:1)

我会改用append child:

var table = document. createElement("table")
document.getElementById("hello").appendChild(table)

......等等

相关问题