简体   繁体   English

Href下载并单击以锚定链接

[英]Href to download and onclick to anchor link

Maybe I'm going about this the wrong way (I wouldn't be too surprised) or maybe it's just not possible. 也许我正在以错误的方式进行操作(我不会太惊讶),或者根本不可能。 I have links that when clicked on, download a file. 我有一些链接,单击这些链接可以下载文件。 I would also like to have these links show a lightbox. 我也想让这些链接显示一个灯箱。 I can easily get them to do one or the other, but not both. 我可以轻松地让他们做一个或另一个,但不能同时做。 The links are also being created through asp. 链接也可以通过asp创建。 The code I have right now: 我现在拥有的代码:

<asp:Label id=Label1 EnableViewState="False" runat="server" Text='<%# "<A  id=""launcher"" class=""track"" Href="""&amp;DataBinder.Eval(Container, "DataItem.Url")&amp;""">"&amp;DataBinder.Eval(Container, "DataItem.Name")&amp;"</A>" %>'>
</asp:Label>

Within the <A> , I have tried: onclick=""test();return false;"" , the same thing except with onclientclick , with and without the return false; <A> ,我尝试过: onclick=""test();return false;"" ,除了onclientclick之外,有和没有return false; (also tried true ). (也尝试true )。

The function I have: 我具有的功能:

function test() {
  window.location.href="#feature";
}

Problem is that it never does the onclick event. 问题在于它永远不会执行onclick事件。 I tried putting a breakpoint at that spot, which gets hit when I click the link, but I never see the content that should be displayed. 我尝试在该位置放置一个断点,当我单击链接时该断点被击中,但是我从未看到应显示的内容。 Is what I'm trying to do even possible, this way or some other way? 通过这种方式或其他方式,我正在尝试做的事情甚至可能吗?

jsFiddle: http://jsfiddle.net/hk3Wd/6/ . jsFiddle: http : //jsfiddle.net/hk3Wd/6/ This is behaving slightly differently than on the website I'm working on. 这与我正在工作的网站的行为略有不同。 On the website, the lightbox shows but no download. 在网站上,灯箱显示但没有下载。 In that fiddle, the download starts but not lightbox. 在这种小提琴中,下载开始但不启动灯箱。

It seems that your "double" double quotes are the issue. 看来您的“双”双引号是问题所在。 This works 这有效

<a href='test.php' onclick='test();return false;'>click me!</a>
<script type='text/javascript'>
function test() {
    window.location.href="#feature";
}
</script>

Thanks to some suggestions from Diodeus and others found online, I have figured this out. 感谢Diodeus的一些建议和其他在线建议,我已经弄清楚了。

Diodeus was correct in that I had two instances of id=launcher , one of which was unnecessary. Diodeus是正确的,因为我有两个id=launcher实例,其中一个是不必要的。 I removed that. 我删除了。

The other was in the way Fancybox seems to work. 另一个是Fancybox的工作方式。 It uses the href attribute to determine what to fill the frame with. 它使用href属性确定用什么填充框架。 In this case href was set to a file (exe), so it was trying to display the executable in the lightbox frame rather than actually downloading it. 在这种情况下,href设置为文件(exe),因此它试图在灯箱框架中显示可执行文件,而不是实际下载它。

To fix this, I set the onclick of the <a> tag to a JS function. 为了解决这个问题,我将<a>标签的onclick设置为JS函数。 That function: 该功能:

function test() {
   $("a.launcher").trigger("click");
}

Within my hidden div, I added: 在隐藏的div中,我添加了:

<a class="launcher" href="#ads"></a>

Basically, the onclick calls the function which triggers the hidden link which launches the lightbox. 基本上,onclick会调用触发隐藏链接的函数,该链接会启动灯箱。

jsFiddle: http://jsfiddle.net/FjgRw/1/ jsFiddle: http : //jsfiddle.net/FjgRw/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM