function make(tagname,attributes,children){
//如果参数的个数为二个,并且属性的类型是数组或是字符串,将当前的属性值赋给孩子
if(arguments.length == 2 &&
(attributes instanceof Array || typeof attributes == "string")){
children = attributes;
attributes = null;
}
//创建了一个元素
var e = document.createElement(tagname);
//为新标签设置属性
if(attributes) {
for(var name in attributes)
e.setAttribute(name, attributes[name]);
}
if(children != null){
if(children instanceof Array){
for(var i = 0; i < children.length; i++){
var child = children[i];
if(typeof child == "string"){
child = document.createTextNode(child);
e.appendChild(child);
}
}
}
else if(typeof children == "string"){
e.appendChild(document.createTextNode(children));
}
else e.appendChild(children);
}
return e;
}
以上是js部分,<button onclick="make('table',{border:1},'tr');">click</button>是body里,现在是怎么让新创建的表格显示在指定位置?还有判断Array和string有什么不同,是通过他们去判断什么吗?
//如果参数的个数为二个,并且属性的类型是数组或是字符串,将当前的属性值赋给孩子
if(arguments.length == 2 &&
(attributes instanceof Array || typeof attributes == "string")){
children = attributes;
attributes = null;
}
//创建了一个元素
var e = document.createElement(tagname);
//为新标签设置属性
if(attributes) {
for(var name in attributes)
e.setAttribute(name, attributes[name]);
}
if(children != null){
if(children instanceof Array){
for(var i = 0; i < children.length; i++){
var child = children[i];
if(typeof child == "string"){
child = document.createTextNode(child);
e.appendChild(child);
}
}
}
else if(typeof children == "string"){
e.appendChild(document.createTextNode(children));
}
else e.appendChild(children);
}
return e;
}
以上是js部分,<button onclick="make('table',{border:1},'tr');">click</button>是body里,现在是怎么让新创建的表格显示在指定位置?还有判断Array和string有什么不同,是通过他们去判断什么吗?
document.getElementById('target').appendChild(e);//e为你make方法返回的table对象
function show(e){
var f = make('table',{border:1},'tr');
document.getElementById("divId").appendChild(f);
}
在html中加了 <div id="divId"></div>,没错,但是还是不展示出来…
if(typeof(obj) == "string") {
//string
} else if(Object.prototype.toString.call(obj) == '[object Array]') {
//Array
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'deepIn.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
//创建任意类型tag
function make(tagname,attributes,children){
//如果参数的个数为二个,并且属性的类型是数组或是字符串,将当前的属性值赋给孩子
if(arguments.length == 2 &&
(attributes instanceof Array || typeof attributes == "string")){
children = attributes;
attributes = null;
}
//创建了一个元素
var e = document.createElement(tagname);
//为新标签设置属性
if(attributes) {
for(var name in attributes)
e.setAttribute(name, attributes[name]);
}
if(children != null){
if(children instanceof Array){
for(var i = 0; i < children.length; i++){
var child = children[i];
if(typeof child == "string"){
child = document.createTextNode(child);
e.appendChild(child);
}
}
}
else if(typeof children == "string"){
e.appendChild(document.createTextNode(children));
}
else e.appendChild(children);
}
//alert(e.innerHTML)
return e;
}
function show(e){
var f = make('table',{border:1},'tr');
document.getElementById("divId").appendChild(f);
}
</script> </head>
<body><!-- 展示新增任意tag -->
<button onclick="show('divId');">click</button>
<div id="divId" style="height:400px;width=200px"></div>
<br>
</body>
</html>
//创建任意类型tag
function make(tagname,attributes,children){
//如果参数的个数为二个,并且属性的类型是数组或是字符串,将当前的属性值赋给孩子
if(arguments.length == 2 && (attributes instanceof Array || typeof attributes == "string")){
children = attributes;
attributes = null;
}
//创建了一个元素
var e = document.createElement(tagname);
//为新标签设置属性
if(attributes) {
for(var name in attributes)
e.setAttribute(name, attributes[name]);
}
if(children != null){
if(children instanceof Array){
var parent = e;
for(var i = 0; i < children.length; i++){
var child = children[i];
if(typeof child == "string"){
child = document.createElement(child);
parent.appendChild(child);
parent = child;
}
}
}
else if(typeof children == "string"){
e.appendChild(document.createTextNode(children));
}
else
e.appendChild(children);
}
return e;
}function show(e){
var f = make('table',{border:1,width:'200px',height:'300px'},['tbody','tr','td']);//这里的tbody是tr的父节点,不加tbody这个table就展示不出来,需要加我下面注释掉的那行
document.getElementById("divId").appendChild(f);
//document.getElementById("divId").innerHTML = document.getElementById("divId").innerHTML;
}