只需单击鼠标左键即可使用 jQuery 更改 HREF
我想在带有 fancybox 的 iframe 中打开一个站点,如果用户选择在新选项卡或窗口中打开该站点的链接,则需要提供不同的布局(完整布局)。
正常的链接会显示整个页面,但如果我在末尾添加 &type=77,我的 CMS 知道它应该提供适合 iframe 的精简版本。如果用鼠标左键单击链接,以下代码将附加“&type=77”。
$('#mylink').mousedown(function(event) {
if(event.which == 1) {
var actualHref = $(this).attr("href");
$(this).attr("href", actualHref + "&type=77");
}
});
这效果很好,但每次点击都会附加该部件。因此,如果我左键单击一次并关闭 fancybox,然后右键单击(在新选项卡中打开),那么我仍然会得到带有附加部分的新 URL。并且它还会在每次点击时附加该部分。
我尝试使用 window.location 来更改 url,而不是更改 url - 这部分起作用,它显示了正确的 url,但它破坏了 fancybox。 fancybox 开始加载,但随后它被重定向到新页面。
也许有更好的方法。提前致谢。
I want to open a Site in an iframe with fancybox, if the user chooses to open the link to that site in a new tab or window, then a different layout(the complete layout) needs to be delivered.
The normal link brings up the full page, but if I add &type=77 to the end, my CMS knows that it should deliver the stripped down version that is suited for the iframe. The following code attaches the "&type=77" if the link is clicked with the left mouse button.
$('#mylink').mousedown(function(event) {
if(event.which == 1) {
var actualHref = $(this).attr("href");
$(this).attr("href", actualHref + "&type=77");
}
});
This works great, but it attaches the part for every click. So, if I left click once and close the fancybox and right click(open in new tab) afterwards, then I still end up with the new URL with the attached part. And it also attaches that part for every click.
I tried to use window.location to the changed url instead of changing the url - that worked partially, it brought up the right url but it broke the fancybox. The fancybox started loading but then it got redirected to the new page.
Maybe there's a much better approach for this. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用.one
方法:更新
当您将插件分配给对象时,Fancy box 可以接受要打开的 href,因此请执行以下操作:(
有些人可能知道比/不喜欢对单个元素使用
each
更好的方法,但我不认为这对于一次通话来说真的很重要)Use the.one
method:Update
Fancy box can accept the href to open when you assign the plugin to the objects, so do something like this:
(Some people might know a better way than/dislike using
each
for a single element, but I don't think it really matters for a single call)我希望这能解决这个问题:
I hope this solves the issue:
jQuery .one() 方法 仅执行一次事件。
jQuery .unbind() 方法 从元素中删除事件处理程序。
您可能还想了解有关鼠标事件的更多信息:
W3C 鼠标事件
哪个与按钮
jQuery .one() method to execute the event only once.
jQuery .unbind() method to remove the event handler from the element.
You may also want to read more about mouse events:
W3C Mouse Events
Which vs Button