你不能改变
Window.status
。为了达到期望的结果,您可以使用location.href
或location.replace
var link = document.getElementById("my-link");
link.addEventListener("click", function(event) {
event.preventDefault(); // Prevent the default link behavior
// Change the href to the new URL
link.href = "https://www.example.com";
// Redirect to the new URL
//window.location.href = link.href;
window.location.replace(link.href);
});
<a id="my-link" href="https://www.google.com" data-mc-cta="1" target="_blank">Click me!</a>
包括左键单击、右键单击和上下文菜单
var link = document.getElementById("my-link");
function redirectLink(event) {
event.preventDefault(); // Prevent the default link behavior
link.href = "https://www.example.com"; // Change the href to the new URL
window.location.replace(link.href); // Redirect to the new URL
}
// Listen for left-click and contextmenu events
link.addEventListener("click", redirectLink);
link.addEventListener("contextmenu", redirectLink);
<a id="my-link" href="https://www.google.com" data-mc-cta="1" target="_blank">Click me!</a>
对于使用类和 for 循环的多个 URL
var links = document.getElementsByClassName("link");
function redirectLink(event) {
event.preventDefault(); // Prevent the default link behavior
this.href = "https://example.com"; // Change the href to the new URL
window.location.replace(this.href); // Redirect to the new URL
}
// Loop through all links with the afflink class and add event listeners
for (var i = 0; i < links.length; i++) {
links[i].addEventListener("click", redirectLink);
links[i].addEventListener("contextmenu", redirectLink);
}
<a href="https://www.google.com" class="link"> Google </a>
<a href="https://www.yahoo.com" class="link"> Yahoo </a>
<a href="https://www.bing.com" class="link"> Bing </a>
只有右键单击所有具有相应URL
的a href
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
links[i].addEventListener("contextmenu", function(event) {
event.preventDefault();
window.location.replace(this.href);
});
}
<a href="https://www.Example.com"> Example </a>
<a href="https://www.wikipedia.com"> Wikipedia </a>
<a href="https://www.bing.com"> Bing </a>
<a href="https://www.stackoverflow.com"> stackoverflow </a>
您可以使用
event.preventDefault
来阻止链接传播,然后使用 javascript 重定向到另一个页面。
这是一个简单的例子:
$(document).ready(function (){
$("a").click(function(event) {
event.preventDefault(); //stop the redirect
location.href="https://www.wikipedia.org";
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="link">
<a href="http://www.google.it">Google</a>
</div>
希望我有帮助!