简体   繁体   English

如何单击链接但不刷新页面?

[英]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.

相关问题 ServiceWorker Notification Click - 如何刷新页面并跳转到锚链接? - ServiceWorker Notification Click - How can I refresh a page AND jump to an anchor link? 单击链接类后如何加载 JavaScript 页面刷新? - How can I load JavaScript page refresh after click on link class? 如何单击链接但不重新加载页面? - How can I click a link but not reload the page? 如何使用JavaScript捕获点击并将页面发送到链接? - How can I use javascript to capture a click and send the page to a link? 如何在JavaScript代码中包含刷新页面链接/按钮? - How can I have refresh page link/button inside my JavaScript code? 如何单击div中的按钮以不刷新整个页面asp.net C# - how can I click on button inside div to not to refresh whole page asp.net C# 我如何单击另一个页面的链接,其中包含当前页面中的功能 - how can i click another page's link which includes functions in current page 当我使用Javascript单击导航菜单链接时,如何更改页面的背景图像? - How can i change background image of the page when i click on navigation menu link using Javascript? 当我点击指向子页面的链接时,如何在页面页脚中保留一个不重新加载的音乐播放器? - How can I keep a music player in the page footer that doesn't reload when I click a link to a subpage? 如何使Meteor React内联svg元素单击链接到页面而不刷新整页 - How to make Meteor React inline svg element click link to page without refresh whole page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM