简体   繁体   English

单击链接类后如何加载 JavaScript 页面刷新?

[英]How can I load JavaScript page refresh after click on link class?

How I load JavaScript main page refresh only after user clicks on specific link class?仅在用户单击特定链接类后,我如何加载 JavaScript 主页刷新? I need it when the user clicks on the link, some JavaScript (or PHP, or whatever) detects that he has clicked on this class link and only then refreshes the origin page.当用户点击链接时,我需要它,一些 JavaScript(或 PHP,或其他)检测到他点击了这个类链接,然后才刷新原始页面。

Example:例子:

When user clicks on:当用户点击:

<a class="link_id" target="blank" href="http://externallink.com">external link</a>

JavaScript below auto load:自动加载下方的 JavaScript:

window.location.reload();

PS: I can not use the onclick="location.reload()" , button or similar method for many reasons (my project particularities, for example). PS:出于多种原因(例如,我的项目特殊性),我无法使用onclick="location.reload()" 、按钮或类似方法。 Moreover, my HTML will be extensive and dirty.此外,我的 HTML 将是广泛而肮脏的。 Moreover, it is not safe for the project to let users know that the refresh is through the onclick direct on link.此外,项目让用户知道刷新是通过 onclick 直接链接是不安全的。 So I'm looking for another way to do this.所以我正在寻找另一种方法来做到这一点。

onclick does exactly what you said. onclick完全按照你说的做。 However, if you really don't want to use it, you can always do this:但是,如果您真的不想使用它,则可以随时执行以下操作:

<a id="link_id" href="javascript:location.reload()">link</a>

As exemplified by this snippet:正如这个片段所示:

 let i = 0;setInterval(() => {document.getElementById("span").innerHTML = i;i++}, 50);
 <a href="javascript:location.reload()">link</a><br> <span id="span"></span>

EDIT : Since you actually told me what you wanted in the comments, I present you with code!编辑:由于您实际上在评论中告诉了我您想要什么,我向您展示了代码!

Since you only have one element with the ID link_id you can do any of the following:由于您只有一个 ID 为link_id元素,您可以执行以下任一操作:

<a id="link_id" href="http://link.com" onclick="location.reload()">link</a>

OR或者

document.getElementById("link_id").onclick = location.reload;

OR或者

document.getElementById("link_id").addEventListener("click", location.reload);

If you have multiple elements under one ID, you seriously need to reconsider your HTML knowledge如果你在一个 ID 下有多个元素,你真的需要重新考虑你的 HTML 知识

YET ANOTHER EDIT : as the air of mystery and vagueness unfolds, I see that you have multiple elements with one ID, or seem to.另一个编辑:随着神秘和模糊的气氛的展开,我看到您有多个元素和一个 ID,或者似乎有。 In that case, use class, and then do this:在这种情况下,使用类,然后执行以下操作:

Array.from(document.getElementsByClassName("<classname>")).forEach(el => {
    el.addEventListener("click", () => {location.reload();});
});

I REALLY HOPE THIS IS THE FINAL EDIT - Replace <classname> with the name of your class, of course我真的希望这是最后一次编辑- 当然,将<classname>替换为您班级的名称

Array.from(document.getElementsByClassName("<classname>")).forEach(el => {
  el.addEventListener("click", () => {
    if (!sessionStorage.getItem("reloaded")) {
      sessionStorage.setItem("reloaded", true);
      location.reload();
    }
  });
});

Check out this jsfiddle - it works: https://jsfiddle.net/gbdu3vpm/1/看看这个 jsfiddle - 它有效: https ://jsfiddle.net/gbdu3vpm/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM