【发布时间】:2021-08-23 15:11:17
【问题描述】:
我已经动态创建了指向用户 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;
}
【问题讨论】:
-
您是否尝试从您链接到的页面的 url 中获取 id?如果是这种情况,那么以下内容可能会对您有所帮助:
var currentUrl = window.location.href -
是的,谢谢您的回答。
标签: javascript api using