如何在javascript中根据用户id动态加载内容



我已经动态创建了一个到用户id的链接当我点击链接页面显示。我想获得数据并将其添加到模板中,以便根据用户id动态加载内容。

function displayPhotographers(jsonfilter){

// jsonfilter est un tableau d'objets

let photographerDetails = "";

jsonfilter.forEach((photographer)=>{ 
const str = photographer.name;
const dash = str.replace(" ", "-");
// Il construit son HTML
photographerDetails += `
<div class="user" id=${photographer.id}>
<a href="/views/page.html?${photographer.id}">
<div class="circle thumb">
<div class="crop">
<img src="img/${photographer.portrait}" alt="" />
</div>
<h2 class="name">${photographer.name}</h2>
</div>
</a>
<p class="city">${photographer.city}</p>
<p class="tagline">${photographer.tagline}</p>
<p class="price">${photographer.price} €/jour</p>
<ul class="tags">
${photographer.tags
.map((tag) =>
`
<li>
<a href="#" class="${tag}">#${tag}</a>
</li>
`.trim()
)
.join("")}
</ul>
</div>
`;
})
return photographerDetails;


}

比如:

function get_id(){
var currentUrl = window.location.href;
var tagLocation = currentUrl.indexof("#");
if(tagLocation<0){
console.warn("no id could be found");
return -1;
}else{
return currentUrl.substr(tagLocation);
}
}

最新更新