javascript 使用javascript在div标签中附加表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12594516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
appending a table in div tag using javascript
提问by user1139525
I have a javascript like
我有一个像 javascript
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(动态创建)
回答by ecco88
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>
回答by MS.
You can define one div tag with ID : "divID" or whatever
您可以定义一个带有 ID 的 div 标签:“divID”或其他任何内容
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. same as array_count_name.
对于 array_count :您可以为动态值获取数组。与 array_count_name 相同。
Thanks.
谢谢。
回答by sureshunivers
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,
以下用于为新创建元素添加属性,
element.setAttribute('attributeName','value'); // or element.attributeName = 'value';
attributeName => attribute name of a element that is class, id, style, align or etc,.
attributeName => 元素的属性名称,即 class、id、style、align 等。
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);