javascript - how to show text to another div id in another page in javascript?
问题描述
I have a form where I have submitted the text from the user input but I dont know how to show the text to a different div in a another page in javascript. can anyone help me solve this issue? thanks for the help.
here is my script:
<input onclick="addTheEvent(); return false;" type="submit" value="Add to list" class="btn btn-primary" />
<script>
var addToTheContent = document.getElementById("canvas");
var scheduleEvent = document.getElementById("scheduleStartTime");
var candidateId = document.getElementById('candId');
var getCandId = document.getElementById("candId");
var displayCandId = getCandId.options[getCandId.selectedIndex].value;
function addTheEvent() {
addToTheContent.innerHTML = "name = " +
displayCandId + " at " + scheduleEvent.value;
}
</script>
Another page: (I want to add the value to show in my content div that is in another page)
<pre id="content" style="white-space: pre-wrap;"></pre>
解决方案
- 您可以使用单页应用程序 (SPA)。
单页应用程序是在浏览器中运行并且在使用过程中不需要重新加载页面的应用程序。
但是SPA是一个相当庞大的主题。它是使用浏览器历史、ajax等知识所必需的。所以你应该尝试使用变体 #2
您可以使用浏览器的本地localStorage来保存数据
localStorage.setItem("key","value") //set value into local storage localStorage.getItem("key") // get value
“key”是任意字符串
在另一个页面上使用
document.addEventListener("DOMContentLoaded", function(event) {
const dataFromLocalStorage = localStorage.getItem('key');
document.querySelector("#content").innerHTML = dataFromLocalStorage; })
推荐阅读
- javascript - 为什么我的日历在 iOS 设备上失败?
- ios - 应用程序未安装在真正的 CarPlay 设备上
- oracle - Oracle Data Modeler 中的对象权限
- python - 如何在多索引中找到索引列?
- python - 将我的 Django docker 容器链接到 postgres 容器
- .net-core - 测试不在 Azure Devops 编译的 aspnet core 3.0 测试项目上运行
- c# - AssertConfigurationIsValid() 使用嵌套的 ConstructUsing 时,不忽略私有 setter
- javascript - 如何将登录凭据从 PHP Apache 服务器发送到 Node.js 服务器
- amazon-web-services - Powershell 无法删除我刚刚写的文件
- python - 在 Pypy 上使用 pip 安装包的问题