Jquery:根据 cookie 值替换所有链接并在单击后替换真实链接
我有一个 cookie 值:clickotV
我有多个具有相同类“t”的 href :
当 cookie 值为 1 时:
我必须通过 out.php 将所有 href 替换为类“t”,并添加目标
_blank
所以我这样做:$(文档).ready(函数(){ if($.cookie('clickotV')==1){ $("at").attr("href", "/out.php"); $("at").attr("目标", "_blank"); } });
但是在用户单击替换的链接(out.php)后我必须替换链接源(没有
_blank
)
我该怎么办?
I have a cookie value : clickotV
I have multiple href with the same class "t" :
<a href="link1.html" class="t">link1</a>
<a href="link2.html" class="t">link2</a>
<a href="link2.html" class="t"><img src="image2.jpg" /></a>
When cookie value is 1 :
I must replace all href with the class "t" by out.php and add target
_blank
So i do this :$(document).ready(function(){ if($.cookie('clickotV')==1){ $("a.t").attr("href", "/out.php"); $("a.t").attr("target", "_blank"); } });
But I must replace the link origin (without
_blank
) after user click on a replaced link (out.php)
How do I ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你需要记住数据,那么你就必须存储它,幸运的是,jQuery 有用于此目的的
.data()
命令。您将遇到的主要问题是在点击后更改
href
。您可以绑定单击事件,设置一个较短的超时以将值更改回来,并在默认操作后取消绑定单击事件。或者尝试用 javascript 打开页面,但这经常被阻止。编辑:更改为在首次点击时重置所有链接。
If you need to remember data, then you will have to store it, fortunately, jQuery has the
.data()
command for this purpose.The major problem you will have is getting the
href
to change after a click. You could either bind the click event, set a short timeout to change the value back and unbind the click event after the default action. Or try and open the page in javascript, but that is often blocked.Edit: Altered to reset all links on first click.