Javascript 仅在单击定位点时生成url
是否可以仅在被单击时将url分配给锚点Javascript 仅在单击定位点时生成url,javascript,php,dynamic-linking,Javascript,Php,Dynamic Linking,是否可以仅在被单击时将url分配给锚点 <a href="http://example.com/">Token Link</a> 当锚被点击时,它将转到 我只想在点击时生成令牌 如果可能,怎么做 谢谢您可以像这样处理事件并更改href $("a").on("click", function() { $(this).attr("href", $(this).attr("href") + "/token=xxxx"); }); 您还可以直接将用户导航到其他url
<a href="http://example.com/">Token Link</a>
当锚被点击时,它将转到
我只想在点击时生成令牌
如果可能,怎么做
谢谢您可以像这样处理事件并更改href
$("a").on("click", function() {
$(this).attr("href", $(this).attr("href") + "/token=xxxx");
});
您还可以直接将用户导航到其他url,而无需更改
$("a").on("click", function(ev) {
document.location.href = "//something-different.com";
ev.preventDefault();
return false;
});
使用jQuery在另一个窗口中打开链接
$(document).ready(function () {
$(".thisClass a").on("click", function(e){
e.preventDefault(); // this prevents going to the original url or default behavior
var changedLink = $(this).attr("href", $(this).attr("href") + "/token=xxxx");
var newUrl = $(changedLink).attr('href');
window.open(newUrl, '_blank');
});
});
//下面是一种使用纯Javascript的方法——我没有在所有浏览器上测试它,但使用了chrome
// goes in a script.js or in script tags under the </body> element
function changeTheLink() {
event.preventDefault();
var aLink = document.getElementById('theLink');
var theOldLink = aLink.getAttribute("href");
aLink.setAttribute('href', theOldLink + "/token=xxxx");
var theNewLink = aLink.getAttribute("href");
window.open(theNewLink, "_blank");
}
// here is the HTML you owuld have to add an id and an onclick attribute to use this code
<div class="thisClass"><a href="http://thiswebsite.com" id="theLink"
onclick="changeTheLink()">Here is a link</a></div>
//位于script.js或元素下的脚本标记中
函数更改链接(){
event.preventDefault();
var aLink=document.getElementById('theLink');
var theollink=aLink.getAttribute(“href”);
aLink.setAttribute('href',theOldLink+“/token=xxxx”);
var theNewLink=aLink.getAttribute(“href”);
打开(新链接,“_blank”);
}
//下面是要使用此代码必须添加id和onclick属性的HTML
是的,你可以通过在链接中添加一个点击处理程序,然后在那里设置窗口。位置
。这就是谷歌搜索跟踪链接的方式,同时当你将鼠标悬停在链接上时,还会在浏览器中显示URL。此外,如果机器人试图欺骗系统以使其网站看起来更受欢迎,他们将获得指向该网站的直接URL,而不是跟踪代码。查看代码,“token=xxxx”在被单击之前就已经存在。或者我错了?当我在jsbin.com中测试它时,我看不到这一点——它不应该将链接的所有操作都定义为“事件处理程序”函数(定义了“单击”处理程序之后的函数())——因此,一旦单击该函数,该函数就开始运行。jsbin示例中的最后一点—您将看到令牌=xxxx在浏览器地址栏中的一秒钟后消失—我使用的url没有该页面,它将其重定向到主页。您可以通过将url复制并粘贴到浏览器中来测试这一点,它将有与我看到的相同的反应。看起来它可以在我的应用程序上运行。仅使用js是否可行。没有jquery?当然可以用普通的JavaScript实现,但是像jquery这样的库就像一把瑞士军刀,可以同时使用所有不同的浏览器。我可以更深入地探讨它——但总的来说,在使用DOM(浏览器API)时,最好使用库。纯Javascript仍然与jQuery-ie混合在一起。在上面的代码中打开…。要使用jQuery,只需将链接放在标题中:
-如果您已经是新的-我的道歉。在代码处添加了一个普通的javascript版本查看,“token=xxxx”或“something different.com”在被单击之前就已经存在了。还是我错了?