问题
I am trying to change the class of a button on one html page by clicking the button on another html page.
- I am a beginner/medium-level programmer
This is the page where I'm supposed to click the button(html and js respectively): output.html
<button class="waves-effect waves-light btn" id = "wutwut"
onclick="testJS()">What Now?</button>
$("#wutwut").click(function() {
console.log("let's go");
var tutorial= $("#replace").html
console.log(tutorial);
$(function testJS(){
var b = document.getElementById(tutorial)
url='https://bili.pythonanywhere.com/tutorialsfinal.html?tutorial=' + encodeURIComponent(b)
document.location.href=url;
console.log("let's get this party started");
});
});
This is the page where the button class is supposed to change(html and js respectively): tutorialsfinal.html
<script> $('.collapsible').collapsible('open', 1);</script>
<ul class="collapsible popout" data-collapsible="accordion">
<li>
<div class="collapsible-header" id="soccer"><i class="material-icons"></i>Soccer</div>
<div class="collapsible-body"><span>Lorem ipsum dolor sit amet.</span></div>
</li>
<li>
<div class="collapsible-header" id="basketball"><i class="material-icons"></i>Basketball</div>
<div class="collapsible-body"><span>Lorem ipsum dolor sit amet.</span></div>
</li>
<li>
<div class="collapsible-header" id="football" ><i class="material-icons"></i>Football</div>
<div class="collapsible-body"><span>Lorem ipsum dolor sit amet.</span></div>
</li>
</ul>
window.onload = function () {
var url = document.location.href,
params = url.split('?')[1].split('&'),
data = {}, tmp;
for (var i = 0, l = params.length; i < l; i++) {
tmp = params[i].split('=');
data[tmp[0]] = tmp[1];
}
function tutorialmatch(){
console.log("let's go");
$('#replace').removeClass('collapsible-header').addClass('collapsible-header active');
tutorialmatch();
console.log(tutorialmatch);
I initially thought this should have worked, but when I run the code, the very first console message("let's get this party started") doesn't show.
Any help/advice is much appreciated! Thank you!
回答1:
Use localStorage.
In the first function set the value
$(function testJS(){
//rest of the code
localStorage.setItem('keyName',value)
document.location.href=url;
console.log("let's get this party started");
});
In the second page retrieve the value using getItem
function tutorialmatch(){
var getValue = localStorage.getItem('keyName')
$('#replace').removeClass('collapsible-header').addClass('collapsible-header active');
}
来源:https://stackoverflow.com/questions/48345175/how-do-i-pass-data-between-2-html-pages-using-a-button