我在浏览器的控制台中遇到此错误消息" Uncaught TypeError:无法设置属性&inner;内部HTML' of null"。
有谁知道如何解决这个问题?
//create a new XMLHttpRequest object
var myXhr = new XMLHttpRequest();
//function will be called when the state of the request changes
myXhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
//single JSON
var film = JSON.parse(myXhr.responseText);
var output = "";
output += "<h1>" + film.title + "</h1>";
output += "<p>" + film.director + "</p>";
var contentDiv = document.getElementById("content");
contentDiv.innerHTML = output;
} else {
alert("Something went wrong.");
}
}
}
//specify the resource we want to access
myXhr.open("get", "index.json", true);
//makes the request
myXhr.send(null);
&#13;
<!doctype html>
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css">
<script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="https://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>My Title</h1>
</div><!-- /header -->
<div data-role="content">
<p>Hello world</p>
</div>
<!-- /content -->
<div data-role="footer">
<h4>My Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
<script src="index.js"></script>
</body>
</html
&#13;
我还使用以下数据制作了一个JSON文件:
[
{
"title": "Jaws",
"year": "1975",
"director": "Steven Spielberg"
},
{
"title": "Mean Streets",
"year": "1973",
"director": "Martin Scorcese"
},
{
"title": "Forrest Gump",
"year": "1994",
"director": "Robert Zemeckis"
}
]
答案 0 :(得分:1)
由于DOM中不存在此content
元素,因此会产生null
,因此您可以添加 id 属性到你的div:
<div data-role="content" id='content'>