简体   繁体   English

如何使用按钮在2个html页面之间传递数据

[英]How do I pass data between 2 html pages using a button

I am trying to change the class of a button on one html page by clicking the button on another html page. 我试图通过单击另一个HTML页面上的按钮来更改一个HTML页面上的按钮的类。

  • 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 这是我应该单击按钮的页面(分别为html和js):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 这是应该更改按钮类的页面(分别为html和js):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! 谢谢!

Use localStorage. 使用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 在第二页中,使用getItem检索值

function tutorialmatch(){
var getValue = localStorage.getItem('keyName')
$('#replace').removeClass('collapsible-header').addClass('collapsible-header active');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM