我正在 Framework7 上创建一个应用程序,并尝试添加一个部分,您可以在其中将文本提交到浏览器的本地存储。如果我使用表单并在 index.html 页面上提交数据,它工作正常,但是如果我在任何其他页面中使用它,我会收到“未捕获的类型错误:无法读取 null 的属性“addEventListener””。我将脚本 src 链接到每个页面底部的同一位置,因此我不知道为什么会收到此错误。
HTML
<form>
Text: <br>
<input type="text" id="Input" name="addText"> <br>
<button id="Submit">Save</button>
</form>
Javascript
var submit = document.getElementById("Submit");
submit.addEventListener("click", function ()
{
var Input = document.getElementById("Input").value;
localStorage.setItem("Text", Input);
alert("Your text has been saved");
console.log("Text saved");
} , false);
最佳答案
Framework7 具有类似 jQuery 的功能,称为 dom7 或 $$。您可以像这样使用该功能:
$$("#Submit").on("click",function() {
var Input = $$("Input").val();
localStorage.setItem("Text", Input);
alert("Your text has been saved");
console.log("Text saved");
});
查找文档 here
关于javascript - 在除我的索引之外的所有页面上收到 'Uncaught TypeError: Cannot read property ' addEventListener' of null' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42999729/