试试这个,看来使用.attr("checked", "checked")
不影响html,尝试使用直接的javascript方法setAttribute
and removeAttribute
$.each(data, function(i, d) {
var cb = theClone.find('.cbClass');
if (d.CB === "True") {
cb.get()[0].setAttribute("checked", "checked");
}
else{
cb.get()[0].removeAttribute("checked");
}
temp += theClone.html();
});
另请注意,因为您始终与theClone
你需要使用removeAttribute
因为上一次迭代修改了该元素。
jsfiddle 上的代码示例 http://jsfiddle.net/markcoleman/q88gw/
Update
如果您直接处理 jQuery 对象,它似乎可以在 IE9、chrome、FF 和 IE 兼容模式下工作。
var temp = [];
if (data !== null) {
$.each(data, function(i, d) {
var theClone = $("#clone div").clone(true);
var cb = theClone.find('.cbClass');
if (d.CB === "True") {
cb.attr("checked", "checked");
}
temp.push(theClone);
});
}
$.each(temp, function(i, item) {
$("body").append(item);
});
jsfiddle 上的代码示例 http://jsfiddle.net/markcoleman/q88gw/2/