jQuery 使用jQuery,如何临时拦截超链接点击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4384382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With jQuery, How Do I Intercept Hyperlink Click Events Temporarily?
提问by Volomike
This question refers to affiliate marketing, but really is a generic question about intercepting hyperlinks before they go off to another site, where you can log visitor activity to a database.
这个问题指的是联属网络营销,但实际上是一个关于在超链接转到另一个网站之前拦截超链接的通用问题,您可以在那里将访问者活动记录到数据库中。
My affiliate marketing client had a really good question. Imagine a scenario where you have products pulled back from Amazon over its API, given a seed keyword. Now imagine a visitor clicks one of those products to view it on Amazon. The URL for that product might look like this (and this is just a demo):
我的联盟营销客户有一个非常好的问题。想象一个场景,在给定种子关键字的情况下,您通过其 API 从亚马逊撤回产品。现在想象一个访问者点击其中一个产品在亚马逊上查看它。该产品的 URL 可能如下所示(这只是一个演示):
http://www.amazon.com/dp/B0042RU3Y0/?tag=xxxxxxxxxxxxxxxx-20
http://www.amazon.com/dp/B0042RU3Y0/?tag=xxxxxxxxxxxxxxxx-20
But guess what's wrong with that? It's not passing that seed keyword. So, we have no idea which seed keywords were the most effective. Instead, she was wishing we could pass the following and then track that somehow:
但你猜这有什么问题?它没有传递该种子关键字。因此,我们不知道哪些种子关键字最有效。相反,她希望我们可以通过以下内容,然后以某种方式跟踪它:
http://www.amazon.com/dp/B0042RU3Y0/?tag=xxxxxxxxxxxxxxxx-20&seed=laptops
http://www.amazon.com/dp/B0042RU3Y0/?tag=xxxxxxxxxxxxxxxx-20&seed=laptops
I didn't see any docs on Amazon where we could pass extra params and then track them in the reports by a filter.
我在亚马逊上没有看到任何可以传递额外参数然后通过过滤器在报告中跟踪它们的文档。
So, the only thing I could think of is that we need to capture the click before it goes off to Amazon. In other words, before that event bubbles up and is executed, somehow in jQuery I can intercept it first, parse the href URL for that hyperlink, tack on this extra seed keyword information, send it over AJAX back to a PHP page and a database table, and then release that click event so that it is executed and the browser goes off to Amazon.
所以,我唯一能想到的就是我们需要在点击进入亚马逊之前捕获点击。换句话说,在该事件冒泡并被执行之前,我可以在 jQuery 中以某种方式首先拦截它,解析该超链接的 href URL,附加这个额外的种子关键字信息,通过 AJAX 将其发送回 PHP 页面和数据库表,然后释放该点击事件,以便它被执行并且浏览器转到亚马逊。
Does anyone know how this is done in jQuery? I know the AJAX part -- just not the click intercept part that grabs the click, then releases it.
有谁知道这是如何在 jQuery 中完成的?我知道 AJAX 部分——只是不是抓住点击然后释放它的点击拦截部分。
采纳答案by Mark
var seed = "&seed=laptops";
$("a").live('click',function(){
$(this).attr('href', $(this).attr('href')+seed);
});
回答by rahul
You can bind a click event to all the anchor tags, like
您可以将点击事件绑定到所有锚标记,例如
$("a").click(function(){
// write your code
});
If you want to do the default action then put a return true;
at the end of this function.
如果要执行默认操作return true;
,请在此函数的末尾放置一个。