ajax做列表,详情页接受参数渲染页面

本文探讨如何在原生JavaScript中实现类似Vue和React框架的页面间参数传递,通过修改`Window.location.href`添加参数,并在详情页使用正则表达式捕获ID,进而根据ID请求数据并动态渲染页面内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   最近在苦思一个问题,我们使用的vue,react等框架,已经是前,后端分离 ,但是在原生js上,我却很少能够做到 页面之间传参,一直认为两页面是独立存在的,跳转打开新页,等于刷新,但是框架能做到的事情,我们也能做得到,毕竟都是用的js实现的...

     //这是首页index.html 写的内容  ,而我们做的详情页跳转就是基于Window.location.href="跳转地址 + 自带参数"  这种形式来实现的

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function(){
var str="";
$.ajax({
type:'post',
url:"https://route.showapi.com/213-1",
data:{
showapi_sign:"b8d3847c26054907944815bc522019cf",
showapi_appid:"40497",
page:"1",
keyword:"海阔天空"
},
success:function(res){
Json(res);
}
})
})
function Json(res){
var res=res.showapi_res_body.pagebean.contentlist;
console.log(res);
for(var i=0; i<res.length; i++){
str="<li>"+res[i].songid+"</li>"
$('body ul').append(str);
};
var li =$("ul li");
li.click(function(){
var index =$(this).index();
console.log($(this).index());
window.location.href="tiao.html?id="+index+"";
})
}
</script>

 //这是我在tiao.html 页面写的东西 , 其中的函数 是用作正则获取链接上的id ;

 最后 就可以实现打印我们传过来的id值 ;

这时候我们就可以通过这个id ,请求不一样的接口,来进行新闻详情上的渲染了

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
var proId =getQueryString('id');
console.log(proId);

// $(function(){
// var str="";
// $.ajax({
// type:'get',
// url:"https://route.showapi.com/213-1",
// data:{
// showapi_sign:"b8d3847c26054907944815bc522019cf",
// showapi_appid:"40497",
// page:"1",
// keyword:"海阔天空"
// },
// success:function(res){
// Json(res);
// }
// })
// })
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

对酒丶当歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值