我有一个显示 KML 文件的谷歌地图(API 3),我创建了一个函数,允许通过带有复选框的控制面板打开或关闭它们。默认情况下,页面加载时会选中复选框,但这在 IE7 或 IE8 中不起作用。我不知道为什么。我试过添加
object.defaultChecked = true;
到每个变量,但这不起作用。我也试过添加这个javascript,但它也不起作用......
var checkboxes = document.getElementsByTagName('input');
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = true;
}
}
IE7-IE8 如何处理 DOM 中的复选框有什么神秘之处吗?任何帮助表示赞赏!
这是我的代码:
function createTogglers() {
var html = "<form><ul>";
for (var prop in kml) {
html += "<li class='selected'><span id=\"selector-" + prop + "\"><input type='checkbox' id='" + prop + "'" +
" onclick='highlight(this,\"selector-" + prop + "\"); toggleKML(this.checked, this.id)' \/></span>" +
kml[prop].name + "<\/li>";
}
html += "<\/ul><\/form>";
document.getElementById("toggle_box").innerHTML = html;
}
function removeAll() {
for (var prop in kml) {
if (kml[prop].obj) {
kml[prop].obj.setMap(null);
delete kml[prop].obj;
}
}
}
// Append Class on Select
function highlight(box, listitem) {
var selected = 'selected';
var normal = 'normal';
document.getElementById(listitem).className = (box.checked ? selected: normal);
}
function startup() {
var checkit = document.getElementById('a');
checkit.checked = true; // Does not work in IE 7-8
checkit.setAttribute('checked', 'checked'); // Does not work in IE 7-8
checkit.checked = 'checked'; // Does not work in IE 7-8
toggleKML(checkit, 'a');
highlight(checkit, 'selector-a');
var checkit2 = document.getElementById('b');
checkit2.checked = true; // Does not work in IE 7-8
checkit2.setAttribute('checked', 'checked'); // Does not work in IE 7-8
checkit2.checked = 'checked'; // Does not work in IE 7-8
toggleKML(checkit2, 'b');
highlight(checkit2, 'selector-b');
var checkit3 = document.getElementById('c');
checkit3.checked = true; // Does not work in IE 7-8
checkit3.setAttribute('checked', 'checked'); // Does not work in IE 7-8
checkit3.checked = 'checked'; // Does not work in IE 7-8
toggleKML(checkit3, 'c');
highlight(checkit3, 'selector-c');
var checkit4 = document.getElementById('d');
checkit4.checked = true; // Does not work in IE 7-8
checkit4.setAttribute('checked', 'checked'); // Does not work in IE 7-8
checkit4.checked = 'checked'; // Does not work in IE 7-8
toggleKML(checkit4, 'd');
highlight(checkit4, 'selector-d');
var checkit5 = document.getElementById('e');
checkit5.checked = true; // Does not work in IE 7-8
checkit5.setAttribute('checked', 'checked'); // Does not work in IE 7-8
checkit5.checked = 'checked'; // Does not work in IE 7-8
toggleKML(checkit5, 'e');
highlight(checkit5, 'selector-e');