首页 > 代码库 > 不要遍历dom

不要遍历dom

function selectProvince() {
$.ajax(
{
type: "post",
url: "/province/getStrType",
data: { "type": "province" },
success: function (msg) {
for (var i = 0; i < msg.length; i++) {
$("#province").append("<option value="http://www.mamicode.com/+ msg[i].CODE +">" + msg[i].PROVINCE + "</option>");
}
selectCity();
}
})
};

 

上面这种写法遍历dom,不好,具体哪不好,以后研究,应该下面的写法

function selectProvince() {
$.ajax(
{
type: "post",
url: "/province/getStrType",
data: { "type": "province" },
success: function (msg) {
alert(msg);

var str;
for (var i = 0; i < msg.length; i++) {
str+="<option value="http://www.mamicode.com/+ msg[i].CODE +">" + msg[i].PROVINCE + "</option>";
}
$("#province").append(str);
selectCity();
}
})
};

不要遍历dom