JavaScript - 将内容写入网页中的任何 DIV
JavaScript - Write content to any DIV in webpage
我正在尝试使用 AJAX 动态构建此站点。我根据之前按下的按钮从 SQL 查询加载 "content" div 的内容。
当我按下"Administrar"按钮的时候,我想给这个"adminButtonContainer"添加一些按钮,但是显示如下错误:
'无法读取 属性 'innerHTML' of nullat XMLHttpRequest.ajaxResponse'
其实我只能往"content"DIV添加新内容了。我一直在阅读与调用 JavaScript 函数相关的内容,一旦每个 DIV 被加载,但我无法理解它。另外,如果可能的话,我想以 "elegant" 的方式完成它:不必在中添加标签以及诸如此类的东西。
这是我的 index.html 文件,我希望它保持简单:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="funciones.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body onload="doAjaxQuery(1,1);">
<!-- LEFT MENU -->
<div class="leftMenu">
<!-- GENERAL BUTTONS -->
<div class="buttonContainer">
<button id="inicio" onclick="buttonPressed(1,0);">Inicio</button>
<button id="artistas" onclick="buttonPressed(2,0);">Artistas</button>
<button id="albumes" onclick="buttonPressed(3,0);">Álbumes</button>
<button id="temas" onclick="buttonPressed(4,0);">Temas</button>
<button id="generos" onclick="buttonPressed(5,0);">Géneros</button>
<button id="plataformas" onclick="buttonPressed(6,0);">Plataformas</button>
</div>
<!-- ONLY-ADMIN BUTTONS -->
<div class="adminButtonContainer">
<button onclick="doAjaxQuery(101,1)">Administrar</button>
</div>
</div>
<div id="content" class="content"></div>
</body>
</html>
这里是 JavaScript 函数,它正确写入 "content" DIV,但在任何其他 DIV:
中抛出错误
function ajaxResponse() {
if (ajaxObject.readyState == 4) {
if (ajaxObject.status == 200) {
// JSON Decodification
var jsonResponse = JSON.parse(ajaxObject.responseText);
var contentDiv = document.getElementById("content");
var leftMenuDiv = document.getElementById("leftMenu");
var buttonsDiv = document.getElementById("buttonContainer");
var adminButtonsDiv = document.getElementById("adminButtonContainer");
// innerHTML works with contentDiv
setNewTitle(jsonResponse.Title);
contentDiv.innerHTML = "";
contentDiv.innerHTML += jsonResponse.H1;
contentDiv.innerHTML += jsonResponse.Body;
// innerHTML won't work with adminButtonsDiv (or any other but contentDiv):
// ERROR: 'Cannot read property 'innerHTML' of nullat XMLHttpRequest.ajaxResponse'
for (let boton of jsonResponse.BotonesAdmin) {
var bSeccion = boton[1];
var bBoton = boton[2];
adminButtonsDiv.innerHTML += bBoton;
}
} else {
document.write('There was an error. HTTP error code ' + ajaxObject.status.toString() + '.');
return;
}
}
}
解决方案 1
此元素中缺少 ID
<div class="adminButtonContainer">
到
<div class="adminButtonContainer" id="adminButtonContainer">
方案二:
如果您不能添加 ID
则将此 JS 行更改为使用 getElementsByClassName
var adminButtonsDiv = document.getElementById("adminButtonContainer");
到
var adminButtonsDiv = document.getElementsByClassName("adminButtonContainer")[0];
我正在尝试使用 AJAX 动态构建此站点。我根据之前按下的按钮从 SQL 查询加载 "content" div 的内容。 当我按下"Administrar"按钮的时候,我想给这个"adminButtonContainer"添加一些按钮,但是显示如下错误:
'无法读取 属性 'innerHTML' of nullat XMLHttpRequest.ajaxResponse'
其实我只能往"content"DIV添加新内容了。我一直在阅读与调用 JavaScript 函数相关的内容,一旦每个 DIV 被加载,但我无法理解它。另外,如果可能的话,我想以 "elegant" 的方式完成它:不必在中添加标签以及诸如此类的东西。
这是我的 index.html 文件,我希望它保持简单:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="funciones.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body onload="doAjaxQuery(1,1);">
<!-- LEFT MENU -->
<div class="leftMenu">
<!-- GENERAL BUTTONS -->
<div class="buttonContainer">
<button id="inicio" onclick="buttonPressed(1,0);">Inicio</button>
<button id="artistas" onclick="buttonPressed(2,0);">Artistas</button>
<button id="albumes" onclick="buttonPressed(3,0);">Álbumes</button>
<button id="temas" onclick="buttonPressed(4,0);">Temas</button>
<button id="generos" onclick="buttonPressed(5,0);">Géneros</button>
<button id="plataformas" onclick="buttonPressed(6,0);">Plataformas</button>
</div>
<!-- ONLY-ADMIN BUTTONS -->
<div class="adminButtonContainer">
<button onclick="doAjaxQuery(101,1)">Administrar</button>
</div>
</div>
<div id="content" class="content"></div>
</body>
</html>
这里是 JavaScript 函数,它正确写入 "content" DIV,但在任何其他 DIV:
中抛出错误function ajaxResponse() {
if (ajaxObject.readyState == 4) {
if (ajaxObject.status == 200) {
// JSON Decodification
var jsonResponse = JSON.parse(ajaxObject.responseText);
var contentDiv = document.getElementById("content");
var leftMenuDiv = document.getElementById("leftMenu");
var buttonsDiv = document.getElementById("buttonContainer");
var adminButtonsDiv = document.getElementById("adminButtonContainer");
// innerHTML works with contentDiv
setNewTitle(jsonResponse.Title);
contentDiv.innerHTML = "";
contentDiv.innerHTML += jsonResponse.H1;
contentDiv.innerHTML += jsonResponse.Body;
// innerHTML won't work with adminButtonsDiv (or any other but contentDiv):
// ERROR: 'Cannot read property 'innerHTML' of nullat XMLHttpRequest.ajaxResponse'
for (let boton of jsonResponse.BotonesAdmin) {
var bSeccion = boton[1];
var bBoton = boton[2];
adminButtonsDiv.innerHTML += bBoton;
}
} else {
document.write('There was an error. HTTP error code ' + ajaxObject.status.toString() + '.');
return;
}
}
}
解决方案 1
此元素中缺少ID
<div class="adminButtonContainer">
到
<div class="adminButtonContainer" id="adminButtonContainer">
方案二:
如果您不能添加 ID
则将此 JS 行更改为使用 getElementsByClassName
var adminButtonsDiv = document.getElementById("adminButtonContainer");
到
var adminButtonsDiv = document.getElementsByClassName("adminButtonContainer")[0];