[英]JavaScript: Invoking click-event of an anchor tag from javascript
I have a page with an anchor tag. 我有一个带锚标记的页面。 In my JavaScript I am setting the HREF
attribute of the anchor tag dynamically based on some if-else conditions. 在我的JavaScript中,我根据一些if-else条件动态设置锚标记的HREF
属性。 Now I want to invoke the click event of the anchor tag programmatically. 现在我想以编程方式调用anchor标记的click事件。 I used the below code, but was not successful. 我使用下面的代码,但没有成功。
var proxyImgSrc="CostMetrics.aspx?Model=" + model +"&KeepThis=true&TB_iframe=true&height=410&width=650";
document.getElementById("proxyAnchor").href=proxyImgSrc;
document.getElementById("proxyAnchor").onclick;
Can any one tell me how to go ahead? 任何人都可以告诉我如何继续吗? I have a jQuery light box implementation(thickbox) on this link. 我在这个链接上有一个jQuery灯箱实现(thickbox)。
Kindly advise. 好心提醒。 Thanks in advance. 提前致谢。
If you have jQuery installed then why not just do this: 如果您安装了jQuery,那么为什么不这样做:
$('#proxyAnchor')[0].click();
Note that we use [0] to specify the first element. 注意,我们使用[0]来指定第一个元素。 The jQuery selector returns a jQuery instance, and calling click() on that only calls click javascript handler, not the href. jQuery选择器返回一个jQuery实例,并调用click(),只调用click javascript handler,而不是href。 Calling click() on the actual element (returned by [0]) will follow the link in an href etc. 在实际元素上调用click()(由[0]返回)将跟随href等中的链接。
See here for an example to illustrate the difference: http://jsfiddle.net/8hyU9/ 请看这里举例说明不同之处: http : //jsfiddle.net/8hyU9/
As to why your original code is not working - it is probably because you are calling onclick
, and not onclick()
. 至于为什么你的原始代码不起作用 - 可能是因为你正在调用onclick
,而不是onclick()
。 Without the parenthesis JavaScript will return whatever is assigned to the onclick
property, not try to execute it. 如果没有括号,JavaScript将返回分配给onclick
属性的任何内容,而不是尝试执行它。
Try the following simple example to see what I mean: 试试下面这个简单的例子来看看我的意思:
var f = function() { return "Hello"; };
alert(f);
alert(f());
The first will display the actual text of the function, while the second will display the word "Hello" as expected. 第一个将显示函数的实际文本,而第二个将按预期显示单词“Hello”。
You should call click event like this: 您应该像这样调用click事件:
document.getElementById("proxyAnchor").click();
// $('#proxyAnchor').click();
but in your case you should set the window's location to a redirect page, if you want that. 但在您的情况下,如果需要,您应该将窗口的位置设置为重定向页面。
I believe you want to invoke the click
event. 我相信你想调用click
事件。 Not the "onClick." 不是“onClick”。 Also, be sure to include the parenthesis () when you're invoking a method. 此外,确保在调用方法时包括括号()。 Don't confuse methods (which end with ( and )) with attributes and properties which do not end with ( and ). 不要将方法(以(和)结尾)与不以(和)结尾的属性和属性混淆。
// Using jQuery - Which you tagged...
$("#proxyAnchor").attr("href", proxyImgSrc).click();
I believe this is what you're after: 我相信这就是你所追求的:
var proxyImgSrc="CostMetrics.aspx?Model=" + model +"&KeepThis=true&TB_iframe=true&height=410&width=650";
$("#proxyAnchor").attr('href', proxyImgSrc).trigger("click");;
For an immediate page change, you can also do this: 要立即更改页面,您还可以执行以下操作:
var proxyImgSrc= "CostMetrics.aspx?Model=" + model + "&KeepThis=true&TB_iframe=true&height=410&width=650";
window.location = proxyImgSrc;
Here's an example from W3 Schools: http://www.w3schools.com/js/tryit.asp?filename=tryjs_location 以下是W3学校的一个例子: http : //www.w3schools.com/js/tryit.asp? filename = tryjs_location
If you are looking for support of IE, then this example below may help: 如果您正在寻找IE的支持,那么下面的示例可能会有所帮助:
suppose you have the blob document in response object: 假设您在响应对象中有blob文档:
var blob = new Blob([response.responseText], { type: headers['content-type'] });
if (navigator.msSaveOrOpenBlob) {
//Launches the associated application for a File or Blob saving for non webkit based browser such as safari or IE
navigator.msSaveOrOpenBlob(blob, "cvSummary.xml");
}
else {
//code for webkit based browser
var link = document.createElement('a');
document.body.appendChild(link);
link.style = "display: none";
var url = window.URL.createObjectURL(blob);
link.href = window.URL.createObjectURL(blob);
link.download = "cvSummary.xml";
link.dataset.downloadurl = ["text/xml", link.download, link.href].join(':');
link.click();
window.URL.revokeObjectURL(url);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.