更改 HREF 属性以右键另存为?
所以我有这个......
$(this).attr("href", "http://site.com/images/downloads/wp-" + $(this).parent().parent().attr("id") + "-1024x768.jpg");
问题是当我右键单击>时 将链接另存为...它没有链接到正确的图像。 我不确定是否可以这样做,但如果有人可以帮助我,我将非常感激。 谢谢!
So I have this...
$(this).attr("href", "http://site.com/images/downloads/wp-" + $(this).parent().parent().attr("id") + "-1024x768.jpg");
The problem is that when I Right Click > Save Link As... it doesn't link to the proper image. I'm not sure if its possible to do this or not, but I would really appreciate it if someone could help me on this. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Firebug 或 DOM Inspector 等诊断工具来检查属性的实际最终值。
对于不同的单击和保存行为的最简单的解释是事件是否被拦截。 “另存为”使用实际的 href 值,而选择链接则可以被拦截(键盘、鼠标向下和向上、单击)以将位置设置为其他内容。
Use a diagnostic tool like Firebug or the DOM Inspector to examine the attribute's actual, final value.
The simplest explanation for differing click and save behaviors is if an event is being intercepted. "Save as" uses the actual href value, whereas selecting the link otherwise can be intercepted (keyboard, mouse down and up, click) to set the location to something else.
您可能会在
$(document).ready()
中进行设置,但您使用$(this)
的事实表明您的代码已绑定到 <的 code>onclick 事件。 这可以解释为什么将鼠标悬停在链接上不会显示更改后的 href,在访问者单击链接之前该值不会更改。
您能否通过
$(document).ready()
的相关部分来证实我的怀疑?如果我是正确的,那么解决方案应该是更改 href 作为
ready()
处理的一部分,而不是当时绑定到 onclick 事件。You might be setting this up in
$(document).ready()
but the fact that you're using$(this)
points to your code being bound to theonclick
event of the<a>
. That would explain why hovering over the link does not show the changed href, the value doesn't get changed until the visitor clicks on the link.Can you should the associated section of your
$(document).ready()
to confirm my suspicions?If I'm correct, then the solution should be to change the href as part of your
ready()
handling, rather than binding to the onclick event at that time.这是一个想法...将“真实”位置放入由 php 生成的 html 中
,然后添加一个带有“假”位置的额外属性,然后使用 jquery 在页面加载时切换它们。 这可以根据情况起作用(即,如果用户禁用了 js 并且它使用“真实”位置,它不会破坏所有内容)
这将解决您的问题并使几乎所有用户的最终结果保持相同: )
here's an idea... put the "real" location in the html generated by php
then add an extra attribute with the "fake" location, and then use jquery to switch them on pageload. This can work depending on the situation (ie. it won't ruin everything if the user has js disabled and it uses the "real" location)
this would solve your problem and keep the end result the same for almost all of your users :)