简体   繁体   English

在<a>代码触发前</a>更改其href

[英]Changing the href of an <a> tag before it fires

My current problem is that I want to change the href of an tag before the page loads what it was previously pointing to. 我当前的问题是,我想在页面加载之前指向的页面之前更改标签的href。

<a href = "#oldLink" onclick = "this.href = #newLink"></a>

I've tried something like this and it didn't work. 我已经尝试过类似的方法,但是没有用。 I've tried: 我试过了:

<a href = "#oldLink" onclick = "changeLink();"></a>

in another file: 在另一个文件中:

changeLink(){
    $("a").attr("href", "#newLink");
}

and this doesn't work, unless you put an alert before the change occurs, and then it does occur as expected. 并且这是行不通的,除非您在更改发生之前发出警报,然后确实按预期发生。 Any help would be greatly appreciated, it's my first time posting so apologies if the format isn't correct 任何帮助将不胜感激,这是我第一次发布,如果格式不正确,我们深表歉意

you can do like this: 您可以这样:

HTML: HTML:

<a href = "#oldLink" id="link"></a>

EIDT: EIDT:

The previous solution will create a recursive loop of event, do like this instead 先前的解决方案将创建一个事件的递归循环,而不是这样做

JQUERY: JQUERY:

$(function () {

$("#link").on("click", function (e) {

    e.preventDefault(); //stop default behaviour

    $(this).attr("href", "#newLink"); // change link href

    window.location.href = "#link2";
  });

});

or: 要么:

$(function () {

$("#link").on("click", function (e) {

    if ($(this).attr('href') === '#oldLink') // check if it is old link
    {

        e.preventDefault(); //stop default behaviour

        $(this).attr("href", "#newLink"); // change link href

        $(this).trigger("click"); // click link programmatically

    }
  });

});

UPDATED: 更新:

$(function () {

    $("#link").on("click", function (e) {

        if ($(this).attr('href') === '#oldLink') // check if it is old link
        {

            $(this).attr("href", "#newLink"); // change link href

            $(this).trigger("click"); // click link programmatically

            return false;

        }
      });

    });

You can redirect a link via JS by something like this 您可以通过JS这样重定向链接

changeLink(e){
    window.location = "#newLink";
    e.preventDefault();
}

You can do by HTML 您可以通过HTML来完成

<a href = "#oldLink">abc</a>

JQuery jQuery查询

$('a').attr("href", "some url");

Your first method was correct. 您的第一种方法是正确的。 It seems that you've just forgotten to quote the text: 似乎您只是忘了引用文本了:

<a href = "#oldLink" onclick = "this.href = '#newLink';"></a>

Don't change the href when the link before the click is processed. 处理点击之前的链接时,请勿更改href Instead, change it earlier , either on page load and/or (if the link opens a new window or otherwise doesn't cause a window reload) after it's clicked. 取而代之的是, 单击页面加载和/或(如果链接打开一个新窗口,否则不会导致窗口重新加载) 之前 ,对其进行更早的更改。

Example: Live Copy 示例: 实时复制

var link = $("#link");

setRandomLink();

link.click(function() {
  // Allow the link to be followed, then update it
  // with a new random value
  setTimeout(setRandomLink, 0);
});

function setRandomLink() {
  link.attr("href", "#" + Math.floor(Math.random() * 100));
}

Note how the link always takes the user to where they can see in advance it's taking them, then updates as appropriate. 请注意,链接如何始终将用户带到他们可以看到的预先位置,然后适当地进行更新。

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

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