我的目标是,而不是简单的重定向!
甚至在您将其标记为“复制”之前,我已经尝试过this、this和this。没起作用。我在下面的代码中尝试过。
<a href="http://google.com" class="test_class">click me</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script language="javascript">
$("document").ready(function() {
setTimeout(function() {
//$('.test_class').click();
$('.test_class').trigger('click');
},10);
});
</script>我尝试过使用和不使用setTimeout的click事件,但没有任何效果。我也试过用身份证,但没起作用。我使用的是google 44.0.2403.157 (64位)在ubuntu14.04上,如果有关系的话。
编辑:--我刚才也尝试过以下几种变体,但它们都没有用:
$(document).ready(function() { //removed quotes.
setTimeout(function() {
//$('.test_class').click();
$('.test_class').trigger('click');
},10);
});这一个
$(document).ready(function() {
setTimeout(function() {
//$('.test_class').click();
$('.test_class')[0].trigger('click'); //added array representation
},10);
});这个点击事件的特点是我可以看到警报,但是<a>的点击事件并没有发生。
<a href="http://google.com" class="test_class">click me</a>
<div class="submit_btn" style="display:none;" onclick="dothis();"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script language="javascript">
function dothis()
{
alert('dskjhfjkds');
$('.test_class').click();
}
$(document).ready(function() {
setTimeout(function() {
//$('.test_class').click();
$('.submit_btn').click();
},10);
});
//$('.test_class')[0].trigger('click');
</script>附注:我的点击事件的实际情况不同,它不是简单的重定向到谷歌网站。
发布于 2015-09-04 19:23:47
取而代之的是这样做:
$('.test_class')[0].click();发布于 2015-09-05 02:53:03
在触发href元素上的click事件时,似乎没有遵循<a>属性。
因此:
<a href="http://google.com" class="test_class">click me</a>
<script>
$(document).ready(function() {
setTimeout(function() {
$('.test_class').click(); // this won't trigger the link to google.com
},10);
});
</script>在下列情况下不会产生任何影响:
<a href="http://google.com" onClick="alert('this is an alert');" class="test_class">click me</a> <!-- notice the onClick attribute -->
<script>
$(document).ready(function() {
setTimeout(function() {
$('.test_class').click(); // this will trigger the alert
},10);
});
</script>这个会的。
我想一定有什么东西阻止href属性的链接被Javascript触发。
如果您想在HTML中执行重定向,我建议您深入研究<meta http-equiv="refresh" content="5; URL=http://www.mydomain.tld">标记。
发布于 2015-09-04 19:24:06
您需要通过访问$('.test_class')[0]来对元素执行DOM事件单击
$('.test_class')[0].click();https://stackoverflow.com/questions/32405183
复制相似问题