尝试使用此代码来模拟对对象的点击,我将此代码注入(inject)到 html 中..
<script type='text/javascript'>
$(window).load(function() {
$('#hellothere').click();
});
</script>
完全没有工作......!
最佳答案
当 DOM 准备就绪时,您应该使用 ready()
函数运行代码 - http://api.jquery.com/ready/ - load()
方法用于加载新内容 - http://api.jquery.com/load/ - 对于您的目的来说是不正确的。然后,您可以使用 trigger()
在 DOM 对象上触发单击事件。
// run when the DOM is loaded
$(document).ready(function(){
$('#hellothere')
// Set up the click event
.on('click', function(){ alert('you clicked #hellothere'); })
// Trigger the click event
.trigger('click');
});
关于javascript - jquery 点击模拟不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13080413/