I know this question has been asked before, but after a search on the web I can't seem to find a straight forward answer.
我知道以前有人问过这个问题,但在网上搜索之后,我似乎找不到一个直接的答案。
the HTML
HTML
<a id=myAnchor href=index.php>
the jQuery (Both of these do not work)
jQuery(两者都不起作用)
$('#myAnchor').click();
or
或
$('#myAnchor').trigger('click');
What is the simplest and most efficient way to achieve this?
最简单、最有效的方法是什么?
7 个解决方案
#1
81
Try this:
试试这个:
$('#myAnchor')[0].click();
It works for me.
它适合我。
#2
31
window.location = document.getElementById('myAnchor').href
#3
24
Click just triggers the click event / events not the actually "goto-the-links-href" action.
单击仅仅触发单击事件/事件,而不是实际的“goto-the-link -href”操作。
You have to write your own handler and then your $('#myAnchor').trigger('click'); will work...
必须编写自己的处理程序,然后编写$('#myAnchor').trigger('click');将工作…
$("#myAnchor").click(function(event)
{
var link = $(this);
var target = link.attr("target");
if($.trim(target).length > 0)
{
window.open(link.attr("href"), target);
}
else
{
window.location = link.attr("href");
}
event.preventDefault();
});
#4
4
<a href="#" id="myAnchor">Click me</a>
<script type="text/javascript">
$(document).ready(function(){
$('#myAnchor').click(function(){
window.location.href = 'index.php';
});
})
</script>
#5
4
I tried few of the above solutions but they didn't worked for me. Here is a link to the page which worked for me automatically click a link
我尝试了上面的一些解决方案,但没有奏效。这是一个链接到页面,它为我自动点击一个链接
Above link has many solutions and here's the one which worked for me,
上面的链接有很多解决方案这里有一个对我有用的,
<button onclick="fun();">Magic button</button>
<!--The link you want to automatically click-->
<a href='http://www.ercafe.com' id="myAnchor">My website</a>
Now within the <script>
tags,
现在在
<script>
function fun(){
actuateLink(document.getElementById('myAnchor'));
}
function actuateLink(link)
{
var allowDefaultAction = true;
if (link.click)
{
link.click();
return;
}
else if (document.createEvent)
{
var e = document.createEvent('MouseEvents');
e.initEvent(
'click' // event type
,true // can bubble?
,true // cancelable?
);
allowDefaultAction = link.dispatchEvent(e);
}
if (allowDefaultAction)
{
var f = document.createElement('form');
f.action = link.href;
document.body.appendChild(f);
f.submit();
}
}
</script>
Copy paste the above code and click on clicking the 'Magic button'
button, you will be redirected to ErCafe.com
.
复制粘贴上面的代码,点击“Magic button”按钮,您将被重定向到ErCafe.com。
#6
2
Add onclick="window.location = this.href"
to your <a>
element. After this modification it could be .click()
ed with expected behaviour. To do so with every link on your page, you can add this:
添加onclick = "窗口。位置=。href"到您的元素。在此修改之后,它可以是.click()ed,具有预期的行为。要实现这一点,你可以在你的页面上添加以下链接:
<script type="text/javascript">
$(function () {
$("a").attr("onclick", "window.location = this.href");
});
</script>
#7
0
If you are using jQuery, you can do it with jQuery.trigger
http://api.jquery.com/trigger/
如果使用jQuery,可以使用jQuery。触发http://api.jquery.com/trigger/
Example
例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
</head>
<body>
<a id="foo" onclick="action()"></a>
<script type="text/javascript">
function action(){
window.location.replace("http://*.com/q/9081426/5526354")
}
$("#foo").trigger("click");
</script>
</body>
</html>
#1
81
Try this:
试试这个:
$('#myAnchor')[0].click();
It works for me.
它适合我。
#2
31
window.location = document.getElementById('myAnchor').href
#3
24
Click just triggers the click event / events not the actually "goto-the-links-href" action.
单击仅仅触发单击事件/事件,而不是实际的“goto-the-link -href”操作。
You have to write your own handler and then your $('#myAnchor').trigger('click'); will work...
必须编写自己的处理程序,然后编写$('#myAnchor').trigger('click');将工作…
$("#myAnchor").click(function(event)
{
var link = $(this);
var target = link.attr("target");
if($.trim(target).length > 0)
{
window.open(link.attr("href"), target);
}
else
{
window.location = link.attr("href");
}
event.preventDefault();
});
#4
4
<a href="#" id="myAnchor">Click me</a>
<script type="text/javascript">
$(document).ready(function(){
$('#myAnchor').click(function(){
window.location.href = 'index.php';
});
})
</script>
#5
4
I tried few of the above solutions but they didn't worked for me. Here is a link to the page which worked for me automatically click a link
我尝试了上面的一些解决方案,但没有奏效。这是一个链接到页面,它为我自动点击一个链接
Above link has many solutions and here's the one which worked for me,
上面的链接有很多解决方案这里有一个对我有用的,
<button onclick="fun();">Magic button</button>
<!--The link you want to automatically click-->
<a href='http://www.ercafe.com' id="myAnchor">My website</a>
Now within the <script>
tags,
现在在
<script>
function fun(){
actuateLink(document.getElementById('myAnchor'));
}
function actuateLink(link)
{
var allowDefaultAction = true;
if (link.click)
{
link.click();
return;
}
else if (document.createEvent)
{
var e = document.createEvent('MouseEvents');
e.initEvent(
'click' // event type
,true // can bubble?
,true // cancelable?
);
allowDefaultAction = link.dispatchEvent(e);
}
if (allowDefaultAction)
{
var f = document.createElement('form');
f.action = link.href;
document.body.appendChild(f);
f.submit();
}
}
</script>
Copy paste the above code and click on clicking the 'Magic button'
button, you will be redirected to ErCafe.com
.
复制粘贴上面的代码,点击“Magic button”按钮,您将被重定向到ErCafe.com。
#6
2
Add onclick="window.location = this.href"
to your <a>
element. After this modification it could be .click()
ed with expected behaviour. To do so with every link on your page, you can add this:
添加onclick = "窗口。位置=。href"到您的元素。在此修改之后,它可以是.click()ed,具有预期的行为。要实现这一点,你可以在你的页面上添加以下链接:
<script type="text/javascript">
$(function () {
$("a").attr("onclick", "window.location = this.href");
});
</script>
#7
0
If you are using jQuery, you can do it with jQuery.trigger
http://api.jquery.com/trigger/
如果使用jQuery,可以使用jQuery。触发http://api.jquery.com/trigger/
Example
例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
</head>
<body>
<a id="foo" onclick="action()"></a>
<script type="text/javascript">
function action(){
window.location.replace("http://*.com/q/9081426/5526354")
}
$("#foo").trigger("click");
</script>
</body>
</html>