addEventListener 不起作用,总是返回 null
2015-01-17
84
我尝试在 Firebase 中设置新值,但总是出现错误
Uncaught TypeError: Cannot read property 'addEventListener' of null
。Firebase.js 已加载到文件头部。
我需要对代码进行哪些更改才能使其正常运行?
index.html
<div class="container">
<div class="form-group">
<input type="text" id="txtUsername" class="form-control">
<input type="text" id="txtBirthday" class="form-control">
<input type="text" id="txtFn" class="form-control">
<button id="btUpdateDb" class="btn btn-primary">Update</button>
</div>
</div>
<script>
var txtUsername = document.getElementById("txtUsername").value,
txtBirthday = document.getElementById("txtBirthday").value,
txtFn = document.getElementById("txtFn").value,
btUpdateDb = document.getElementById("btUpdateMessage"),
rootRef = new Firebase("https://test5421234.firebaseio.com"),
ref = new Firebase("https://test5421234.firebaseio.com/people"),
usersRef = ref.child("users");
btUpdateDb.addEventListener("click", function () {
"use strict";
usersRef.set(txtUsername);
});
</script>
1个回答
您没有正确选择此元素。具体来说,您没有传递按钮的 ID。因此您收到此错误
此
btUpdateDb = document.getElementById("btUpdateMessage"),
应更改为此
btUpdateDb = document.getElementById("btUpdateDb"),
,这样它就能正常工作了。
Christos
2015-01-17