试图使用javascript DOM插入TABLE,无法正常工作 [英] Trying to use javascript DOM to insert TABLE, not working

查看:42
本文介绍了试图使用javascript DOM插入TABLE,无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试使用DOM将一个TABLE插入DIV标签。


任何人都可以告诉我在这里做错了什么?


代码看起来很简单,但它不起作用。


谢谢,
Dave

------------------------------------ --------------------------

[HTML]< HTML>

< ; BODY>

< SCRIPT>

函数InitPage(){


var text1 = document.createTextNode(" cell内容)


var td1 = document.createElement(" TD")

td1.appendChild(text1)

var tr1 = document.createElement(" TR")

tr1.appendChild(td1)

var table1 = document.createElement(" TABLE")

table1.appendChild(tr1)


document.getElementById(" text")。appendChild(table1)

}

< / SCRIPT>


< DIV id = text> x< / DIV>

< BR>

< INPUT type = button onclick = InitPage()value = go!>

< / BODY>

< / HTML> [/ HTML]

Hi,

I''m trying to use the DOM to insert a TABLE into a DIV tag.

Can anyone tell me what I''m doing wrong here?

The code looks straightforward, but it is not working.

Thanks,
Dave
--------------------------------------------------------------
[HTML]<HTML>
<BODY>
<SCRIPT>
function InitPage() {

var text1 = document.createTextNode("cell contents")

var td1 = document.createElement("TD")
td1.appendChild(text1)
var tr1 = document.createElement("TR")
tr1.appendChild(td1)
var table1 = document.createElement("TABLE")
table1.appendChild(tr1)

document.getElementById("text").appendChild(table1 )
}
</SCRIPT>

<DIV id=text>x</DIV>
<BR>
<INPUT type=button onclick=InitPage() value=go!>
</BODY>
</HTML>[/HTML]

推荐答案





我正在尝试使用DOM将一个TABLE插入DIV标签。


任何人都可以告诉我我的意思这里做错了吗?


代码看起来很简单,但它不起作用。


谢谢,

戴夫

---------------------------------------- ----------------------

[HTML]< HTML>

< BODY>

< SCRIPT>

函数InitPage(){


var text1 = document.createTextNode(" cell contents")


var td1 = document.createElement(" TD")

td1.appendChild(text1)

var tr1 = document。 createElement(" TR")

tr1.appendChild(td1)

var table 1 = document.createElement(" TABLE")

table1.appendChild(tr1)

document.getElementById(" text")。appendChild(table1 )

}

< / SCRIPT>


< DIV id = text> x< / DIV>

< BR>

< INPUT type = button onclick = InitPage()value = go!>

< / BODY>

< / HTML> [/ HTML]
Hi,

I''m trying to use the DOM to insert a TABLE into a DIV tag.

Can anyone tell me what I''m doing wrong here?

The code looks straightforward, but it is not working.

Thanks,
Dave
--------------------------------------------------------------
[HTML]<HTML>
<BODY>
<SCRIPT>
function InitPage() {

var text1 = document.createTextNode("cell contents")

var td1 = document.createElement("TD")
td1.appendChild(text1)
var tr1 = document.createElement("TR")
tr1.appendChild(td1)
var table1 = document.createElement("TABLE")
table1.appendChild(tr1)

document.getElementById("text").appendChild(table1 )
}
</SCRIPT>

<DIV id=text>x</DIV>
<BR>
<INPUT type=button onclick=InitPage() value=go!>
</BODY>
</HTML>[/HTML]



您好,

您的代码正在与我合作。你得到了什么错误?

你在网页上得到文本''单元格内容'吗?


问候,

IE中的RP

Hi,
Your code is working with me. what error do you get?
Are you getting text ''cell contents'' on web page?

Regards,
RP


你必须附加到< tbody> ...看看这里针对某些浏览器特定的不一致...


亲切的问候
in IE you have to append to the <tbody> ... have a look here for some browser specific inconsitencies ...

kind regards





非常感谢您花时间回复我的查询。我通过将TBODY添加到表创建代码中使我的代码在IE 6中工作。

但是,即使表中有TBODY,代码也无法在Netscape或Firefox中运行。实际上,在下面的代码中,语句**** document.getElementById(" TableDiv")。appendChil d(table1)****停止执行NS 6浏览器(没有错误消息)。 />

有人知道为什么吗?


如果这是一个明显的问题,我道歉。


非常感谢很多,

戴夫


这里是更新后的代码:

----------

[HTML]< HTML>

< HEAD>

< SCRIPT>


function InitPage(){


var text1 = document.createTextNode(" START")

var td1 = document.createElement(" TD")

td1.appendChild(text1)

var tr1 = document.createElement(" TR")

tr1.appendChild(td1)

var tbody1 = document.createElement(" TBODY")

tbody1.appendChild(tr1)

var table1 = document.createElement(" TABLE" )

table1.appendChild(tbody1)

document.getElementById(" TableDiv")。appendChild(ta ble1)

}


< / SCRIPT>

< / HEAD>


< BODY>

< DIV id = tablediv>< / DIV>

< BR>

< INPUT type = button onclick = InitPage()value = go!>

< / BODY>

< / HTML> [/ HTML]
Hi,

Thanks very much for taking the time to respond to my query. I got my code to work in IE 6 by adding the TBODY to the table creation code.
However, even with the TBODY in the table, the code doesn''t work in Netscape or Firefox. In fact, in the code below, the statement ****document.getElementById("TableDiv").appendChil d(table1)**** halts execution of the NS 6 browser (there is no error message).

Would anybody know why?

I apologize if this is an obvious queston.

Thanks very much,
Dave

Here''s the updated code:
----------
[HTML]<HTML>
<HEAD>
<SCRIPT>

function InitPage() {

var text1 = document.createTextNode("START")
var td1 = document.createElement("TD")
td1.appendChild(text1)
var tr1 = document.createElement("TR")
tr1.appendChild(td1)
var tbody1 = document.createElement("TBODY")
tbody1.appendChild(tr1)
var table1 = document.createElement("TABLE")
table1.appendChild(tbody1)

document.getElementById("TableDiv").appendChild(ta ble1)
}

</SCRIPT>
</HEAD>

<BODY>
<DIV id=tablediv></DIV>
<BR>
<INPUT type=button onclick=InitPage() value=go!>
</BODY>
</HTML>[/HTML]


这篇关于试图使用javascript DOM插入TABLE,无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆