我在获取单击了的href标记的属性值时遇到了问题,当我单击这个href标记是正在显示的值时,但是当我的浏览器将我发送到peoplegallery_album时,id现在变为null,我认为浏览器刷新这就是为什么这个值变为null的原因。这样的情况下,反应什么也没有显示出来。<a href="/peoplegallery_album/'+gallery_id+'" class="clicked_albums" data-id='+gallery_id+' style="color:black; font-weight: 500;">'+gallery_content_title+'</a>\
问题:为什么在浏览器将我发送到第二页之后,我的第一个点击项--值显示--没有得到任何响应,是因为我的浏览器刷新了吗?
,我将向您展示单击项返回值.的示例。

现在我创建了api来列出peoplegallery相册中所有项目的id.
$(document).ready(function(){
$(document).on('click','.clicked_albums',function(e) {
var album_id= $(this).data('id');
alert(album_id);
$.ajax({
url:'http://localhost:8000/api/peoplegallery_album/'+ album_id,
type:'get',
data: {id: album_id},
dataType: "json",
contentType: "application/json",
processData: true,
success:function(response) {
console.log(response);
},
error:function(response) {
console.log(response);
}
});
});
}); 因此,在浏览器将我发送到另一个页面后,我没有得到任何响应。。

发布于 2019-02-08 12:57:51
使用localStorage代替ajax
//This page
function a(e)
{
var t=e.getAttribute('id');
localStorage.setItem('id',t);
}
//Other page
var k=localStorage.getItem('id');
//for deleting
function b()
{
localStorage.removeItem('id');
}<a id="23" onclick="a(this)" href="#">This had id 23</a>
<a id="23" onclick="b()" href="#">delete localstorage</a>
发布于 2019-02-08 13:05:14
这是链接元素的预期行为。它带您到在'href‘属性中指定的URL。
如果要防止位置更改,请添加
e.preventDefault()在此之前
var album_id= $(this).data('id');解析数据的正确方法是在页面的初始加载时进行。。
可以通过下一种方式实现这一目标:
每次单击链接时,您的页面将被刷新,循环将重复。
https://stackoverflow.com/questions/54592900
复制相似问题