[英]How can I click a link but not refresh the page?
how can I use ajax with links and not refreshing the page?如何使用带有链接的 ajax 而不刷新页面? This code shows the links of brands
此代码显示品牌的链接
<div class="row">
<div class="col-md-12">
<div class="offer-tabs">
<ul class="nav nav-tabs" id="offerTab" role="tablist">
<li class="nav-item" >
<a class="nav-link active" id="all-tab" href="accueil2.php?r=All" >Toutes les marques</a>
<?php
$query="select * from marque";
$query_run=mysqli_query($conn,$query);
while ($row=mysqli_fetch_assoc($query_run)) {
?>
</li>
<li class="nav-item" >
<a class="nav-link brand" href="accueil2.php?ro=<?php echo $row['id_marque'] ?>"><?php echo $row['name'];?></a>
<?php } ?>
</ul>
I think you might want to look into javascript on the client.我想您可能想在客户端上查看 javascript。
For the following HTML:对于以下 HTML:
<div>
<a id="someId">Get data</a>
</div>
// Find the element
const link = document.getElementById("someId");
// attach an onclick listener
link.addEventListener("click", async (e) => {
e.preventDefault(); // This prevents the navigation
// Get data
const response = await fetch(`/accueil2.php?ro${id_marque}`);
// Convert data to usable text
const data = await response.text();
// Replace the contents of the div
link.parentElement.innerHtml = data;
};
This is not the best way perse, but I believe it achieves what you want.这不是最好的方式,但我相信它可以实现你想要的。 You'll have to get the id_marque as a variable from somewhere.
您必须从某处获取 id_marque 作为变量。
I'm not really sure of what you asked... But if you want to open a link in a new tab, just add the target
argument at your <a>
element, using the _blank
value.我不太确定您问了什么...但是如果您想在新选项卡中打开链接,只需使用
_blank
值在<a>
元素中添加target
参数。
Eg例如
<a href="https://google.com" target="_blank">Google</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.