如何使用 javascript 自动点击 link
How to get a automatically clicked link with javascript
我创建了一个 link 到 rederect.html 的按钮,并在其中使用 <a href>
创建了一个 link必须自动点击才能转到 index.html.
因此,当您在索引上并使用按钮到达那里时,您会转到 rederect.html 并自动返回到 index.html 因为 a href
必须自动触发。唯一的问题是 <a href>
不会自动触发。
这是我目前得到的:
<a href="index.html" class="banan" id="banan">Succesvol uitgelogt return naar login.</a>
<script type="text/javascript">
window.onload = function() {
$(function(){
window.location.href = $('.banan').attr('index.html');
});
}
</script>
我使用 window.onload
事件尝试触发其中的函数,以便在您进入重定向页面时触发。但它不会 autorederect/click <a href>
或返回任何错误所以我真的不知道如何解决这个问题。
要使用函数 .attr
获取属性的 值,您需要传递属性 name
即 href
。
The .attr() method gets the attribute value for only the first element in the matched set
所以:
window.location.href = $('.banan').attr('href');
如果您已经在使用 jQuery,只需使用 $(location).attr('href',url);
。 window.location.href
似乎在某些浏览器中有不稳定的行为,事实上,它在我的 Firefox 版本中完全不起作用。我听说直接设置 window.location 在 IE 版本中不起作用。
试试这个:-
document.getElementById('yourLinkID').click();
尝试这样的事情怎么样-
<a href="index.html" class="banan" id="banan">Succesvol uitgelocht return naar login.</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
window.onload = function() {
window.location.href = $('.banan').attr('href');
});
}
</script>
这对我有用!
我创建了一个 link 到 rederect.html 的按钮,并在其中使用 <a href>
创建了一个 link必须自动点击才能转到 index.html.
因此,当您在索引上并使用按钮到达那里时,您会转到 rederect.html 并自动返回到 index.html 因为 a href
必须自动触发。唯一的问题是 <a href>
不会自动触发。
这是我目前得到的:
<a href="index.html" class="banan" id="banan">Succesvol uitgelogt return naar login.</a>
<script type="text/javascript">
window.onload = function() {
$(function(){
window.location.href = $('.banan').attr('index.html');
});
}
</script>
我使用 window.onload
事件尝试触发其中的函数,以便在您进入重定向页面时触发。但它不会 autorederect/click <a href>
或返回任何错误所以我真的不知道如何解决这个问题。
要使用函数 .attr
获取属性的 值,您需要传递属性 name
即 href
。
The .attr() method gets the attribute value for only the first element in the matched set
所以:
window.location.href = $('.banan').attr('href');
如果您已经在使用 jQuery,只需使用 $(location).attr('href',url);
。 window.location.href
似乎在某些浏览器中有不稳定的行为,事实上,它在我的 Firefox 版本中完全不起作用。我听说直接设置 window.location 在 IE 版本中不起作用。
试试这个:-
document.getElementById('yourLinkID').click();
尝试这样的事情怎么样-
<a href="index.html" class="banan" id="banan">Succesvol uitgelocht return naar login.</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
window.onload = function() {
window.location.href = $('.banan').attr('href');
});
}
</script>
这对我有用!