使用layui实现从商品展示跳转到商品详情页,上一页html跳转到另外一页html,前端值在页面之间的传递

18 篇文章 0 订阅
17 篇文章 0 订阅

一. 问题背景

  • 点击商品展示页,能另外打开新标签跳转到商品详情页,效果如下:
    在这里插入图片描述

  • 前台:使用到的页面全是html没有jsp,所以这里是在html之间传值,从商品展示页传递商品id到商品详情页

二. 解决方案

2.1 思路

  1. <a>绑定onclick事件
  2. 定义一个处理跳转的函数

2.2 关键代码

最最最最最关键的是window.open('xxxxxxxxxxxxx.html');

  • mallIndex.html商品详情页,product_id是已经被赋值了的变量,将此值传给商品详情页,如下:
<a onclick='to(product_id)'></a>
<script>
window.to = function (id){
   window.open('productDetail.html?product_id='+id);
}
</script>

解释:

  1. 如果上面用window.location.href而不是window.open,那么将不会打开新的标签页进行跳转,而是在当前标签页进行跳转。window.open的作用有target='_blank'的功能
  2. 上面定义函数用window.to = function (){} 而不是function to(){},如果采用后者,那么点击<a>后则会出现Uncaught ReferenceError: xxx is not defined报错
  • productDetail.html商品详情页,最最最最关键是用window.location.search(),如下:
<script>
var product_id = oneValue();

//接收单个值
function oneValue(){
    var result;
    var url = window.location.search();//获取url中“?”后面的字符串   
    if(url.length != -1){
         result = url.substr(url.indexOf("=")+1);
         console.info("result:" + result);//按浏览器F12,打印出来
    }
    return result;
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值