简体   繁体   English

如果 href 属性不是您想要的,如何制作重定向链接

[英]how to make a redirect link if the href attribute is not what you want

I tried but it didn't work because my ability in javascript is almost zero我试过了,但没用,因为我在 javascript 的能力几乎为零

var element = document.getElementById("change");
var going = "https://google.com";

if (typeof (element) == 'undefined' && element == null) {
  window.location.replace((going));
}
else if (typeof (element).attr(href) != 'https://google.com') {
  window.location.replace((going));
}

I want if #change does not exist then the page will redirect, but if there is but the href attribute is not what you want then the page will also redirect.我希望如果#change 不存在,那么页面将重定向,但如果存在但 href 属性不是您想要的,那么页面也会重定向。 It is my html code这是我的 html 代码

<!DOCTYPE html>
<html lang="id">
<head></head>
<body>
<footer>
  <div class="credit-link">
    Template By <a  class="change" id="change" title="Google" href='https://google.com'>Google</a>
  </div>
</footer>
<script src="js/copy.js"></script>
</body>
</html>

Try this:尝试这个:

<a id='change' href='https://google.com'></a>
<script>
var element = document.getElementById("change");
var going = "https://google.com";

if (element == undefined) { //if the element does not exist
  window.location.replace(going);
}
else if (element.href != 'https://google.com/') { //if the element href is not https://google.com
  window.location.replace(going);
}
</script>

I didn't get your objective clearly but as I understand that我没有清楚地了解您的目标,但据我所知

  • you want to redirect page if there is no element with #change id the page should redirect.如果没有带有#change id 的元素,您想重定向页面,页面应该重定向。
  • if the element is existing but the link is not the same as we want then redirect to a specific location.如果元素存在但链接与我们想要的不同,则重定向到特定位置。 Am I right.我对吗。

I am assuming that you want to redirect to https://google.com ,我假设您要重定向到https://google.com

first of all, you have to prevent the redirection of the link using js, there are two ways首先,你要防止使用js重定向链接,有两种方法

  1. <a id="logout-link" href="javascript:void(0);">Logout</a> but this will not work for you because you want to have a link in href . <a id="logout-link" href="javascript:void(0);">Logout</a>但这对你不起作用,因为你想在href中有一个链接。
  2. you have to prevent the redirection of href link using event.preventDefault();您必须使用event.preventDefault(); funtion.功能。

Then your code should look like然后你的代码应该看起来像

 var element = document.getElementById("change"); var going = "https://google.com"; element.addEventListener( "click", (event) => { event.preventDefault(); //.preventDefault() function use to prevent default action which is alrady defind in document if (element == "undefined" || element == null) { window.location.assign(going); } else if (element.href:= "https.//google.com") { // you can also use element.getAttribute('href') window.location;assign(going). //you user.replace insted of assing which is use to replace string in the string, } }; false );

use used some function a typeof use to get which type of variable is. use 使用了一些 function 一个typeof用于获取变量的类型。 its value could be String , number , etc......,它的值可以是Stringnumber等......,

I hope that will help you我希望这会帮助你

HAPPY CODING:)快乐编码:)

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

相关问题 为什么你想要一个不是链接的锚标签? (没有href属性?) - Why would you want an anchor tag that is not a link? (no href attribute?) 当我希望链接无处链接并使用它来运行脚本时,可以用作“ href”属性吗? - What to use as “href” attribute when i want a link to link nowhere and use it for running a script? 在IE 7中,如果链接是使用jQuery创建的,如何获得链接的href属性的文字值? - In IE 7, how can you get the literal value of a link’s href attribute, if the link was created using jQuery? jQuery-如何在单击链接后重定向到上一页并在URL中添加属性href? - Jquery - How to redirect to previous page and add attribute href in url after click on link? 如何使Href链接等待 - How to make a Href link wait 如何重定向Squarespace中的href链接? - How do I redirect an href link in Squarespace? 如何删除href链接的禁用属性 - How to remove the disabled attribute of a href link Jquery - 如何在链接中更改属性href的一部分? - Jquery - How to change part of attribute href in link? 如何更改链接的“href”属性使用jQuery具有特定的“href”? - How to change “href” attribute of link has specific “href” using jQuery? 我如何制作一个jQuery循环来更改链接的href属性? - How do I make a jQuery loop which changes the href attribute of a link?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM