单击时如何使用javascript/jquery更改任何本地链接的目标? [英] How to change the target of any local link when clicked, using javascript / jquery?

查看:77
本文介绍了单击时如何使用javascript/jquery更改任何本地链接的目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击本地链接时,我需要更改其href. 我已经解决了如何选择正确的链接并创建新的url,但是不确定如何更改位置.我需要确定其他所有点击事件也都在运行,所以我不能只是立即更改位置.

I need to change the href of any local link when it is clicked on. I've worked out how to select the correct links and create the new url, but not sure how to change the location. I need to be certain that any other click events have also run, so I can't just change the location immediately.

var my_host = 'example.com';
$(document).click(function(e) {
    if(e.target.hostname == my_host)
    {
        alert(e.target.protocol + "//" + e.target.hostname + "/#" + e.target.pathname + e.target.search);
        //...
    }
});

这与我的早期问题有关. /p>

This is related to my earlier question.

推荐答案

您可以像这样将事件绑定到本地链接上,并使用attr方法更改href:

You can bind the event on your local links like this and use the attr method to change the href:

$('a[href*="yourdomain.com"]').click(function(){
  // your code before changing href
  $(this).attr('href', 'new url here');
});

yourdomain.com替换为您自己的域,以使上面的代码仅定位到您的本地链接.

Replace yourdomain.com with your own domain so that the above code only targets your local links.

这篇关于单击时如何使用javascript/jquery更改任何本地链接的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆