如何使用JQuery/JavaScript通过单击div将文本从一个页面传递到另一个页面

如何使用JQuery/JavaScript通过单击div将文本从一个页面传递到另一个页面,javascript,jquery,html,Javascript,Jquery,Html,我正在做一个学校项目,我在这里呆了将近一个星期。我想得到的是。。。我有两页 第1页有五个以上不同标题的div。我想复制用户单击的特定标题,并将其粘贴到第2页。我尝试了很多方法,但仍然没有得到答案 • • 使用本地存储或会话存储: $('div').on('click', function() { localStorage.text = $(this).find('a').val(); //or sessionStorage.text }); 及 首先使用jQuery在脚本上创建页

我正在做一个学校项目,我在这里呆了将近一个星期。我想得到的是。。。我有两页 第1页有五个以上不同标题的div。我想复制用户单击的特定标题,并将其粘贴到第2页。我尝试了很多方法,但仍然没有得到答案


•
•

使用本地存储会话存储

$('div').on('click', function() {
    localStorage.text = $(this).find('a').val(); //or sessionStorage.text
});

首先使用jQuery在脚本上创建页面处理程序:

var pg
switch (pg) {
        case 'page1':
            //the code for page1
            break;
        case 'page2':
            //the code for page2
            break;
    }
<script>pg = 'page1';<script>
//the code for page1
$('div').on('click', function() {
    localStorage.text = $(this).find('a').val();
});
<script>pg = 'page2';<script>
//the code for page2
$('.result-show-here').html(localStorage.text);
在您的情况下:

第1页: 在您的HTML页面上

var pg
switch (pg) {
        case 'page1':
            //the code for page1
            break;
        case 'page2':
            //the code for page2
            break;
    }
<script>pg = 'page1';<script>
//the code for page1
$('div').on('click', function() {
    localStorage.text = $(this).find('a').val();
});
<script>pg = 'page2';<script>
//the code for page2
$('.result-show-here').html(localStorage.text);
第2页:

在您的HTML页面上

var pg
switch (pg) {
        case 'page1':
            //the code for page1
            break;
        case 'page2':
            //the code for page2
            break;
    }
<script>pg = 'page1';<script>
//the code for page1
$('div').on('click', function() {
    localStorage.text = $(this).find('a').val();
});
<script>pg = 'page2';<script>
//the code for page2
$('.result-show-here').html(localStorage.text);
根据你的情况

我更喜欢在a标签上添加一个类,如
London
并获得正确的值:

//the code for page1
    $('div').on('click', function() {
        localStorage.text = $(this).find('.city').val();
    });

使用本地存储会话存储

$('div').on('click', function() {
    localStorage.text = $(this).find('a').val(); //or sessionStorage.text
});

首先使用jQuery在脚本上创建页面处理程序:

var pg
switch (pg) {
        case 'page1':
            //the code for page1
            break;
        case 'page2':
            //the code for page2
            break;
    }
<script>pg = 'page1';<script>
//the code for page1
$('div').on('click', function() {
    localStorage.text = $(this).find('a').val();
});
<script>pg = 'page2';<script>
//the code for page2
$('.result-show-here').html(localStorage.text);
在您的情况下:

第1页: 在您的HTML页面上

var pg
switch (pg) {
        case 'page1':
            //the code for page1
            break;
        case 'page2':
            //the code for page2
            break;
    }
<script>pg = 'page1';<script>
//the code for page1
$('div').on('click', function() {
    localStorage.text = $(this).find('a').val();
});
<script>pg = 'page2';<script>
//the code for page2
$('.result-show-here').html(localStorage.text);
第2页:

在您的HTML页面上

var pg
switch (pg) {
        case 'page1':
            //the code for page1
            break;
        case 'page2':
            //the code for page2
            break;
    }
<script>pg = 'page1';<script>
//the code for page1
$('div').on('click', function() {
    localStorage.text = $(this).find('a').val();
});
<script>pg = 'page2';<script>
//the code for page2
$('.result-show-here').html(localStorage.text);
根据你的情况

我更喜欢在a标签上添加一个类,如
London
并获得正确的值:

//the code for page1
    $('div').on('click', function() {
        localStorage.text = $(this).find('.city').val();
    });

如果您的页面由某种Web服务器托管,您可以使用它来通信/共享数据

如果在没有任何服务器的情况下在同一台机器/浏览器上打开页面,我建议使用浏览器会话或本地存储对象。 要设置值,请使用:

sessionStorage.myValue = 'value'
var val = sessionStorage.myValue
要获得值,请使用:

sessionStorage.myValue = 'value'
var val = sessionStorage.myValue

看看。

如果您的页面由某种Web服务器托管,您可以使用它来通信/共享数据

如果在没有任何服务器的情况下在同一台机器/浏览器上打开页面,我建议使用浏览器会话或本地存储对象。 要设置值,请使用:

sessionStorage.myValue = 'value'
var val = sessionStorage.myValue
要获得值,请使用:

sessionStorage.myValue = 'value'
var val = sessionStorage.myValue

看看。

显示您的代码尝试“页面”是什么意思?你试过什么?我怎么给你看。我是新来的这是另一个标签上的页面吗?如果是,请尝试使用localstorage:Show your code Attempt的可能副本“页面”是什么意思?你试过什么?我怎么给你看。我是新来的这是另一个标签上的页面吗?如果是,请尝试使用localstorage:可能重复的