Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

javascript - Get data from one html file and display it using another html file

I have 2 html files. I want to take the innerHTML of an element of the 1st HTML file and display it in the 2nd HTML.

This is the 1.html file

<h1 id="data">DATA</h1>
<button onclick="go()">GO</button>

This is the 2.html file

<h1 id="display"></h1>

This is what I tried in js but didn't work

function go() {
    let data = document.getElementById("data").innerHTML;
    document.getElementById("display").innerHTML = data;
    window.open("2.html")
}

Please someone help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is code which may help you. In 1.html:

<h1 id="data">Main</h1>
<a href="2.html">
<button onclick="go()">GO</button>
</a>
<script src="action.js"></script>

In 2.html:

<h1 id="display"></h1>
<script src="action.js"></script>
<script>disp();</script>

In action.js:

function go() {
  var data = document.getElementById("data").innerHTML;
  localStorage.setItem("data ", data );
}

function disp() {
  document.getElementById("display").innerHTML = localStorage.getItem("data");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...