如何使用按钮在2个html页面之间传递数据 [英] How do I pass data between 2 html pages using a button

查看:58
本文介绍了如何使用按钮在2个html页面之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过单击另一个HTML页面上的按钮来更改一个HTML页面上的按钮的类.

I am trying to change the class of a button on one html page by clicking the button on another html page.

  • 我是初学者/中级程序员

这是我应该单击按钮的页面(分别为html和js):output.html

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");

    });

});

这是应该更改按钮类的页面(分别为html和js):tutorialsfinal.html

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!

推荐答案

使用localStorage.

Use localStorage.

在第一个函数中设置值

$(function testJS(){
        //rest of the code
        localStorage.setItem('keyName',value)
        document.location.href=url;
        console.log("let's get this party started");

    });

在第二页中,使用getItem检索值

In the second page retrieve the value using getItem

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

这篇关于如何使用按钮在2个html页面之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆