简体   繁体   English

有2个页面,如何通过第一个按钮上的click事件更改第二个页面的内容?

[英]Having 2 pages, How to change the content of a second page by a click event on a button in the first one?

Here is my problem, I have a page called page1 (I have a button in this page to access another page called page2). 这是我的问题,我有一个名为page1的页面(此页面中有一个按钮可以访问另一个名为page2的页面)。 I want to change the content of a specific selector in page2 by a click event on the button of page1. 我想通过page1的按钮上的click事件更改page2中特定选择器的内容。 Here is my code for page 1 in pug 这是我的帕格第1页的代码

 form#abandonForm(name='btn' action='page2')
  button#abandonBtn.button Abandonner

Here is my code for page2 in pug 这是我在哈巴狗中page2的代码

section#termineExam

and here is my .js file 这是我的.js文件

$( document ).ready(function() {
  var $abandonBtn = $('#abandonBtn');
  $abandonBtn.click(function(){
    $.get('page2', null, function(data){
    var $data = $(data);
    console.log(data);
    console.log($data.find('#termineExam').html());
    $data.find('#termineExam').text("section changed");
    console.log($data.find('#termineExam').html()); // print the changes
    console.log(data); // print the same data without changes on the section

})
});
});

I don't understand why changes are applied on the DOM but not on the page2 itself. 我不明白为什么将更改应用于DOM而不应用于page2本身。 Thank you for helping and sorry for my English! 感谢您的帮助,对不起我的英语!

You seem to misunderstand how jQuery.get() works. 您似乎误解了jQuery.get()工作方式。

From the docs: 从文档:

jQuery.get( url [, data ] [, success ] [, dataType ] ) jQuery.get(url [,data] [,success] [,dataType])

Returns: jqXHR 返回值:jqXHR

Description: Load data from the server using a HTTP GET request. 说明:使用HTTP GET请求从服务器加载数据

Basically, you are getting the content from 'page2' as a string and storing it in a variable named data . 基本上,您是从'page2'中获取内容作为字符串并将其存储在名为data的变量中。 This is a one way street. 这是单行道。 When you call $data.find('#termineExam').text("section changed"); 当您调用$data.find('#termineExam').text("section changed"); you are modifying the local variable not the actual page2 file 您正在修改本地变量, 而不是实际的page2文件

When you navigate to page2 , you are requesting a brand new copy of the page2 file without any of the modifications that you applied to that string over on page1. 当导航到page2 ,您请求的是page2文件的全新副本,而没有在page1上对该字符串应用的任何修改。

If you want to actually modify the content of page2 , you'll need some sort of server side script (like php) that accepts new data from page1 and updates the page2 file. 如果要实际修改page2的内容,则需要某种服务器端脚本(例如php),该脚本接受page1的新数据并更新page2文件。

Alternatively, if you just want to send some data to page 2 and have that data appear when that page loads, there are many ways you could do that. 另外,如果您只想向第2页发送一些数据并在该页面加载时显示该数据,则可以采用多种方法。

One option would be to use localstorage : 一种选择是使用localstorage


Make a file called storage.js with the below content and include this file in both page1 and page2 创建一个具有以下内容的名为storage.js的文件,并将此文件同时包含在page1page2

/**
 * Feature detect + local reference for simple use of local storage
 * if (storage) {
 *    storage.setItem('key', 'value');
 *    storage.getItem('key');
 * }
 *
 */
var storage;
var fail;
var uid;
try {
  uid = new Date;
  (storage = window.localStorage).setItem(uid, uid);
  fail = storage.getItem(uid) != uid;
  storage.removeItem(uid);
  fail && (storage = false);
} catch (exception) {}
/* end  Feature detect + local reference */

In page1 do this: 在第1 page1执行以下操作:

$(document).ready(function() {
  var $abandonBtn = $('#abandonBtn');
  $abandonBtn.click(function() {
      if(storage){
        storage.setItem('termineExam', 'section changed');
      }
  });
});

Then, in page2 do this: 然后,在page2做到这一点:

$(document).ready(function() { 
      if(storage){
        var text = storage.getItem('termineExam');
        if(text) $('#termineExam').text(text);
      }
});

暂无
暂无

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

相关问题 如何更改 $("#button").one("click", first ); 到 $("#button").one("click",second); 通过点击一个按钮 - how to change $("#button").one("click", first ); to $("#button").one("click",second); by clicking on a button 如何在按钮单击时更改页面内容或部门内容 - How to change page content or division content on button click 三页一键点击事件 - On one button click event for three pages jQuery:button_click事件在第一次单击时不触发,而在第二次单击时起作用,为什么? 如何解决? - jQuery : button_click event not fires on first click and works on second click, why? How to fix it? 如何在单击时更改按钮而无需在 React 中刷新页面 - How to make button change on click without having to refresh page in React 将一个功能更改为第二个按钮单击 - change one function into second in button click 如何使用angularjs在单击按钮时更改html页面中的垂直内容? - how to change a perticular content in html page on button click using angularjs? 如何防止第二次单击按钮上的单击事件 - How to prevent click event on second times on a button 如何在选择第二个单选按钮时取消选中第一个单选按钮,并在有 2 个单选组时取消选中? - How can uncheck the first radio button when selecting the second one and reverse when having 2 radio groups? 第一次尝试时,链接点击事件未触发按钮点击事件(仅限IE)。 第二次单击即可工作。 - link click event not firing button click event on the first try (IE only). Works on second click.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM