简体   繁体   English

使用javascript在div标签中追加表格

[英]appending a table in div tag using javascript

I have a javascript like 我有一个像

function btnclick()
{

  \\creating a div dynamically
  div[divclick,300,200, 'ok', cancel, btnok_click, btncancel_click]

}

<div style display:none>
<table id = 'tblcontainer'>
<tr>
<td>
add:
</td>
<asp:label id = lblgo>
</asp:label>
</tr>
</table>

Now on executing the btnclick fucntion.I need to append the tblcontainer to divclick(dynamically created) 现在执行btnclick功能。我需要将tblcontainer附加到divclick(动态创建)

your javascript is not real and your html is malformed, so I am assuming it to be pseudocode. 您的JavaScript不是真实的,并且您的html格式不正确,所以我假设它是伪代码。

function btnclick(){
        \\creating a div dynamically
        var div = document.createElement("DIV");
        \\ you can do the other stuff not sure where you got the following...
        \\  div[divclick,300,200, 'ok', cancel, btnok_click, btncancel_click]
        div.appendChild(document.getElementById('tblcontainer'));
}

<div style display:none>
<table id = 'tblcontainer'>
<tr>
<td>
add:
</td>
<asp:label id = lblgo>
</asp:label>
</tr>
</table>

You can define one div tag with ID : "divID" or whatever 您可以定义一个ID为“ divID”或其他的div标签

Then, 然后,

var adddivAll = document.getElementById("divID");
adddivAll.innerHTML = showData();

Function 功能

function showData()
{
   var str;

   str = "<table width='300' border='0' cellspacing='0' cellpadding='0' align='left'>";

   str += "<tr><th width='55'>Rang</th><th width='80'>Name</th></tr>";

   for (i = 0; i < array_count; i++) {
       str += "<tr><td width='55'>" + array_count[i] + "</td><td width='85'>" + array_count_Name[i] + "</td></tr>";
   }

   str += "</table>";

   return str;
}

For array_count : You can take an array for dynamic values. 对于array_count:您可以将一个数组用于动态值。 same as array_count_name. 与array_count_name相同。

Thanks. 谢谢。

Syntax for creating new element is, 创建新元素的语法是,

var element= document.createElement(TAG_NAME);

TAG_NAME is a string example values are "DIV", "H1", "SPAN", etc,. TAG_NAME是字符串示例,值是“ DIV”,“ H1”,“ SPAN”等。

Following use to add attributes for the new create element, 在用于为新的create元素添加属性之后,

element.setAttribute('attributeName','value'); // or element.attributeName = 'value';

attributeName => attribute name of a element that is class, id, style, align or etc,. attributeName =>元素的属性名称,该元素是类,标识,样式,对齐方式等。

Add content into created element: 将内容添加到创建的元素中:

var content = document.getElementById('tblcontainer').outerHTML;
element.innerHTML = content;

For the content variable directly we can provide element string values. 对于content变量,我们可以直接提供元素字符串值。

Attach the created element on body or any other div element, 将创建的元素附加到body或任何其他div元素上,

var _div = document.getElementById("divId");
_div.appendChild(element);
//or
var _body = document.getElementsByTagName('body')[0];
_body.appendChild(element);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM