我正在使用mysql
模块从数据库中获取一些数据并尝试将它们写入HTML文档,但document.getElementById
语句似乎无法在查询语句。这是代码
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<div id="wtv">YOU LOSE, SIR!</div>
<script>=
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "MYUSERNAME",
password: "MYPASSWORD",
database: "travel"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT rooms.*, hotels.name AS hotel_name FROM rooms JOIN hotels ON rooms.hotel_id = hotels.id", function (err, result, fields) {
if (err) throw err;
makeHTML(result);
});
});
function makeHTML(list) {
var outp = "<ul>lolz";
for (i = 0; i < list.length; i++) {
outp += "<i><ul><p>Room "
+list[i].room_num.toString()
+" at "+list[i].hotel_name+".</p><i>Price: "
+list[i].price.toString()+"<br></i><i>Balconies: "
+list[i].balconies.toString()+"</i></ul></i><br><br>";
};
outp += "</ul>";
document.getElementById("wtv").innerHTML = outp;
}
</script>
</body>
</html>
div内容不会更新,我对javascript很新,所以我可能会遗漏一些相当明显的内容。提前谢谢。