HTML错误无法设置null属性(匿名函数) [英] HTML error cannot set property of null (anonymous function)
本文介绍了HTML错误无法设置null属性(匿名函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将文本框值存储到本地存储,但是我捕获到了Uncaught TypeError:无法将属性'onclick'设置为null,并且不确定确切原因.一切似乎都被正确引用.任何见识将不胜感激.
I'm trying to store a textbox value to local storage however I'm catching an Uncaught TypeError: Cannot set property 'onclick' of null and not sure exactly why. Everything seems to be referenced correctly. Any insight would be appreciated.
<script type="text/javascript">
var save_button = document.getElementById('Save')
save_button.onclick = saveData;
function saveData()
{
var input = document.getElementById("saveServer");
localStorage.setItem("server", input.value);
var storedValue = localStorage.getItem("server");
};
</script>
<label for="serveri">Server:</label>
<input type='text' name="server" id="saveServer" />
<button onclick="saveData()" type="button" value="Save" id="Save">Save</button>
如果以上内容没有显示出我的问题,那么JSFiddle中的内容如下: http://jsfiddle.net/mGfeu/
If the above doesn't show my problem, here is the whole in JSFiddle: http://jsfiddle.net/mGfeu/
推荐答案
在正文之后编写脚本.执行脚本时未加载DOM.因此,没有ID为保存"的元素.
Write the script after the body. The DOM isn't loaded when your script executes. Hence there is no element with id 'Save'.
这篇关于HTML错误无法设置null属性(匿名函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文