gpt4 book ai didi

javascript - 代码在 jsfiddle 中有效,但在 html 文件中无效

转载 作者:太空宇宙 更新时间:2023-11-04 16:05:09 25 4
gpt4 key购买 nike

此代码在 fiddle 中有效,但在 html 文件中无效:

    <!doctype html>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js">
var editElem = document.getElementById("editor1");
$(document).ready(function() {
$("#savebtn").click(function() {
var title = prompt("What would you like your title to be?");
localStorage.setItem(title, editElem.value);
titles = localStorage.getItem("titles");

if (titles == null) {
titles = [];
} else {
titles = JSON.parse(titles);
}
titles.push(title);
localStorage.setItem("titles", JSON.stringify(titles));
document.getElementById("update").innerHTML = "Edits saved!";
var htmlData = "<a onclick=showData('" + title + "')>" + title + "</a><br>";
$("#Contentable").append(htmlData);
var userVersion = editElem.value;
localStorage.setItem("userEdits", userVersion);
editElem.value = "";
});
});

function showData(txt) {
editElem.value = localStorage.getItem(txt);
}
</script>
</head>
<body>
<input type='text' id="editor1" />
<input type='button' id="savebtn" value="save" />
<div id="Contentable"></div>
<br>
<br>
<br>
<br>
<br>
<div id="update"></div>
</body>
</html>

fiddle :https://jsfiddle.net/4uwvcrdc/我已经检查了 chrome 中的控制台;没有错误。我也把脚本移到了我的 body 下面,还是不行。

最佳答案

链接外部 js 文件的小修正。改变

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js">

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js">
</script>

<script>

<!doctype html>
<html>

<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
<script>
var editElem = document.getElementById("editor1");
$(document).ready(function() {
$("#savebtn").click(function() {
var title = prompt("What would you like your title to be?");
localStorage.setItem(title, editElem.value);
titles = localStorage.getItem("titles");

if (titles == null) {
titles = [];
} else {
titles = JSON.parse(titles);
}
titles.push(title);
localStorage.setItem("titles", JSON.stringify(titles));
document.getElementById("update").innerHTML = "Edits saved!";
var htmlData = "<a onclick=showData('" + title + "')>" + title + "</a><br>";
$("#Contentable").append(htmlData);
var userVersion = editElem.value;
localStorage.setItem("userEdits", userVersion);
editElem.value = "";
});
});

function showData(txt) {
editElem.value = localStorage.getItem(txt);
}
</script>
</head>

<body>
<input type='text' id="editor1" />
<input type='button' id="savebtn" value="save" />
<div id="Contentable"></div>
<br>
<br>
<br>
<br>
<br>
<div id="update"></div>
</body>

</html>

关于javascript - 代码在 jsfiddle 中有效,但在 html 文件中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com