-
1. appendChild方式(JS方法)
<ul id="parentUl"></ul>
function addElementImg(obj) {
var ul = document.getElementById(obj);
//添加 li
var li = document.createElement("li");
//添加 img
var img = document.createElement("img");
//设置 img 属性,如 id
img.setAttribute("id", "newImg");
//设置 img 图片地址
img.src = "/images/prod.jpg";
li.appendChild(img);
ul.appendChild(li);
}
name为参数名称
addElementImg("parentUl");
-
2.innerHtml方式 (JS方法)
<div id="container">
</div>
<script>
window.onload = function () {
var str='';
for(var i=0; i<30 ;i++){
var r = parseInt(Math.random()*100); //随机生成一个数字
str += '<div>'+r+'</div>'; //拼接str
}
document.getElementById('container').innerHTML=str;
}
</script>
-
3.可以选择左右插入位置方法 (JS方法)
<body>
<div id="div-input">
<input type="text" id="txt_input" value="" />
<input type="button" id="leftInsert" value="左侧入" />
<input type="button" id="rightInsert" value="右侧入" />
</div>
<div id="container">
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
<script>
window.onload = function() {
var inputValue = document.getElementById('txt_input').value;
document.getElementById('leftInsert').onclick = function() {
//左侧入
var span = document.createElement('span'); //1、创建元素
span.innerHTML = inputValue;
var container = document.getElementById('container'); //2、找到父级元素
container.insertBefore(span, container.childNodes[0]); //插入到最左边
}
document.getElementById('rightInsert').onclick = function() {
//右侧入
var span = document.createElement('span'); //1、创建元素
span.innerHTML = inputValue;
var container = document.getElementById('container'); //2、找到父级元素
container.appendChild(span); //3、在末尾中添加元素
}
}
</script>
-
4.appendTo方式创建(JQuery方式)
- 1.方式一
function CreateDom() {
var select = $("<select/>").appendTo($("body"));
var option1 = $("<option value=\"1\">text1</option>").appendTo(select);
var option2 = $("<option value=\"2\">text2</option>").appendTo(select);
var option3 = $("<option value=\"3\">text3</option>").appendTo(select);
var text = $("<input type=\"text\">").css({ "width": "150px", "border": "1px lightgrey solid" }).appendTo($("body"));
var checkbox = $("<input type=\"checkbox\" />").appendTo($("body"));
var ul = $("<ul/>").appendTo($("body"));
var li = $("<li>li1</li>").appendTo(ul);
var li = $("<li>li2</li>").appendTo(ul);
}
- 2.方式二
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function() {
$('<input />', {
id: 'cbx',
name: 'cbx',
type: 'checkbox',
checked: 'checked',
click: function() {
alert("点我了~~");
}
}).appendTo($('#wrap'));
});
</script>
</head>
<body>
<div id="wrap"></div>
</body>
-
5.可以选择左右插入位置方法 (JS方法)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
var a = buildHTML("a", "我是由模版生成的", {
id: "myLink",
href: "http://www.baidu.com"
});
$('#wrap1').append(a);
var input = buildHTML("input", {
id: "myInput",
type: "text",
value: "我也是由模版生成的~~"
});
$('#wrap2').append(input);
});
buildHTML = function(tag, html, attrs) {
// you can skip html param
if (typeof (html) != 'string') {
attrs = html;
html = null;
}
var h = '<' + tag;
for (attr in attrs) {
if (attrs[attr] === false) continue;
h += ' ' + attr + '="' + attrs[attr] + '"';
}
return h += html ? ">" + html + "</" + tag + ">" : "/>";
};
</script>
</head>
<body>
<div id="wrap1"></div>
<div id="wrap2"></div>
</body>
-
6.innerHTML 方式创建(JQuery方式)
success: function (msg) {
if (msg.ret) {
var str = "";
var data = msg.data;
for (i in data) {
str += "<tr>" +
"<td>" + data[i].hotel_seq + "</td>" +
"<td>" + data[i].hotel_name + "</td>" +
"<td>" + data[i].order_no + "</td>" +
"<td>" + data[i].user_phone + "</td>" +
"<td>" + data[i].create_time + "</td>" +
"<td>" + data[i].user_id + "</td>" +
"<td>" + data[i].cellid + "</td>" +
"<td>" + data[i].gps_city + "</td>" +
"<td>" + data[i].cell_city + "</td>" +
"<td>" + data[i].distance + "</td>" +
"</tr>";
}
tbody.innerHTML = str;
}
},
error: function () {
alert("查询失败")
}
});
网友评论