我是Javascript的新手,我正在尝试从Admin表单动态地将列表元素添加到用户表单中。但我做不到。有人能帮我吗。
User.html页面代码:
<html>
<head>
<title>User Page</title>
</head>
<body>
<ul id="dynamic-list"></ul>
<script src="script.js"></script>
</body>
</html>Admin.html页面代码:
<html>
<head>
<title>Admin Page</title>
</head>
<body>
<input type="text" id="candidate"/>
<button onclick="addItem()">add item</button>
<button onclick="removeItem()">remove item</button>
<script src="script.js"></script>
</body>
</html>Javascript代码:
function addItem(){
var ul = document.getElementById("dynamic-list");
var candidate = document.getElementById("candidate");
var li = document.createElement("li");
li.setAttribute('id',candidate.value);
li.appendChild(document.createTextNode(candidate.value));
ul.appendChild(li);
}
function removeItem(){
var ul = document.getElementById("dynamic-list");
var candidate = document.getElementById("candidate");
var item = document.getElementById(candidate.value);
ul.removeChild(item);
}发布于 2019-11-05 11:50:26
尝试使用localStorage或sessionStorage方法将值设置为setItem,尽管这不是一种理想的方法。从管理页面中设置该项,并在其他页面上获取该项,这可以是简单的,每次刷新时都设置,而不是异步,对于您的101应用程序来说,这非常简单。另外,每次在设置getItems之前,都要维护一个id或count,这样您就可以迭代它或比较复制。因此,在行政管理方面,如:
sessionStorage.setItem('key', 'value')在那里维护一个计数变量
sessionStorage.getItem('count')虽然sessionStorage也给出了一个长度变量,但是类似的东西可以用于您的游戏,尽管我强烈建议使用API。
https://stackoverflow.com/questions/58710350
复制相似问题