javascript 加载时触发点击功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10840714/
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
Trigger click function on load
提问by user1038814
I have the following function which is activated on click:
我有以下功能,点击激活:
$('.results .view-rooms').click(function(){ }
Is there anyway I can trigger this function on document load?
无论如何我可以在文档加载时触发此功能吗?
回答by Kevin B
Yes.
是的。
$(document).ready(function(){ // on document ready
$(".results .view-rooms").click(); // click the element
})
回答by gdoron is supporting Monica
$('.results .view-rooms').click()
You can put it in DOM ready:
你可以把它放在 DOM 中:
$(function(){
$('.results .view-rooms').click()
});
Or window load
:
或window load
:
$(window).load(function(){
$('.results .view-rooms').click();
});
Note that there is no such event document load
.
We have DOM ready
or window load
请注意,没有此类事件document load
。
我们有DOM ready
或window load
回答by Pethical
$(document).ready(function(){ $('.results .view-rooms').click(); });
回答by Anthony Grist
Considering that you're already using jQuery to bind the event handler, and assuming that code is already in a position where the entire DOM has been constructed, you can just chain the call to .click()
to then trigger that event handler:
考虑到您已经在使用 jQuery 绑定事件处理程序,并假设代码已经在构建整个 DOM 的位置,您可以将调用链接.click()
到然后触发该事件处理程序:
$('.results .view-rooms')
.click(function(){...}) //binds the event handler
.click(); // triggers the event handler
回答by Anthony Grist
Put the code inside
把代码放在里面
$(function(){ // code here });
like:
喜欢:
$(function(){
$(".results .view-rooms").click();
});
or
或者
$(function(){
$(".results .view-rooms").trigger('click');
});
回答by Consule
The best way is:
最好的办法是:
html form:
html表格:
<form action="https://stackoverflow.com" method="get">
<button id="watchButton"></button>
</form>
End Jquery:
结束jQuery:
<script>
$('document').ready(function() {
$('#watchButton').click();
});
</script>
JQuery Version:
jQuery 版本:
https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
回答by Alfred Larsson
$(function(){
$('.results .view-rooms').click(function(){
}
$(".results .view-rooms").trigger('click');
}