jQuery 或 JavaScript 自动点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery or JavaScript auto click
提问by eMRe
How can I auto click on the link on page load? I have been trying for ages but id does not work.
如何在页面加载时自动点击链接?我已经尝试了很长时间,但 id 不起作用。
<link rel="stylesheet" type="text/css" href="leightbox.css" />
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="leightbox.js"></script>
</head>
<body>
<div class="content">
<p> <a href="#" class="lbOn" rel="pop02">Click here to activate leightbox popup.</a></p>
</div>
<!----------// POPUP (ON CLICK) //---------->
<div id="pop02" class="leightbox">
<a href="#" class="lbAction" rel="deactivate">×</a>
<div class="scrollbox">
<h1>This popup loads upon clicking a trigger link.</h1>
text</div>
</div>
</body>
回答by Zack Bloom
You haven't provided your javascript code, but the usual cause of this type of issue is not waiting till the page is loaded. Remember that most javascript is executed before the DOM is loaded, so code trying to manipulate it won't work.
您尚未提供 javascript 代码,但此类问题的常见原因不是等到页面加载完毕。请记住,大多数 javascript 是在加载 DOM 之前执行的,因此尝试操作它的代码将不起作用。
To run code after the page has finished loading, use the $(document).ready callback:
要在页面加载完成后运行代码,请使用 $(document).ready 回调:
$(document).ready(function(){
$('#some-id').trigger('click');
});
回答by Edukondalu Thaviti
First i tried with this sample code:
首先,我尝试使用此示例代码:
$(document).ready(function(){
$('#upload-file').click();
});
It didn't work for me. Then after, tried with this
它对我不起作用。然后之后,试过这个
$(document).ready(function(){
$('#upload-file')[0].click();
});
No change. At last, tried with this
没变。最后,试过这个
$(document).ready(function(){
$('#upload-file')[0].click(function(){
});
});
Solved my problem. Helpful for anyone.
解决了我的问题。对任何人都有帮助。
回答by r0m4n
$(document).ready(function(){
$('.lbOn').click();
});
Suppose this would work too.
假设这也会起作用。
回答by eMRe
$(document).ready(function(){
$('#some-id').trigger('click');
});
did the trick.
成功了。
回答by Fenton
In jQueryyou can trigger a click like this:
在jQuery 中,您可以触发这样的点击:
$('#foo').trigger('click');
More here:
更多在这里:
http://api.jquery.com/trigger/
http://api.jquery.com/trigger/
If you want to do the same using prototype, it looks like this:
如果你想使用原型做同样的事情,它看起来像这样:
$('foo').simulate('click');
回答by Boris Hamanov
You are trying to make a popup work maybe? I don't know how to emulate click, maybe you can try to fire click event somehow, but I don't know if it is possible. More than likely such functionality is not implemented, because of security and privacy concerns.
您正在尝试制作一个弹出窗口吗?我不知道如何模拟点击,也许您可以尝试以某种方式触发点击事件,但我不知道是否可行。由于安全和隐私问题,很可能没有实现此类功能。
You can use div with position:absolute to emulate popup at the same page. If you insist creating another page, I cannot help you. Maybe somebody else with more experience will add his 15 cents.
您可以使用带有 position:absolute 的 div 来模拟同一页面上的弹出窗口。如果您坚持创建另一个页面,我无法帮助您。也许其他更有经验的人会增加他的 15 美分。