使用 jQuery 在页面加载时自动单击按钮元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18646881/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:08:04  来源:igfitidea点击:

Auto-click button element on page load using jQuery

jqueryhtmlbuttonmodal-dialogpageload

提问by JStormThaKid

If I wanted to auto-click a button element on page load, how would I go about this using jQuery?

如果我想在页面加载时自动单击一个按钮元素,我将如何使用 jQuery 来解决这个问题?

The button html is

按钮 html 是

<button class="md-trigger" id="modal" data-modal="modal"></button>

Any help on this topic would be greatly appreciated! Thanks in advance!

任何有关此主题的帮助将不胜感激!提前致谢!

回答by JStormThaKid

You would simply use jQuery like so...

你会像这样简单地使用jQuery......

<script>
jQuery(function(){
   jQuery('#modal').click();
});
</script>

Use the click function to auto-click the #modal button

使用点击功能自动点击#modal按钮

回答by KingRider

JavaScript Pure:

纯 JavaScript:

<script type="text/javascript">
document.getElementById("modal").click();
</script>


JQuery:

查询:

<script type="text/javascript">
$(document).ready(function(){
    $("#modal").trigger('click'); 
});
</script>

or

或者

<script type="text/javascript">
$(document).ready(function(){
    $("#modal").click(); 
});
</script>

回答by user3164444

Use the following code

使用以下代码

$("#modal").trigger('click');

回答by Ravi Mane

I tried the following ways in first jQuery, then JavaScript:

我首先在 jQuery 中尝试了以下方法,然后在 JavaScript:

jQuery:

jQuery

 window.location.href = $(".contact").attr('href');
 $('.contactformone').trigger('click');  

This is the best way in JavaScript:

这是JavaScript 中最好的方法:

 document.getElementById("id").click();

回答by mhtmalpani

We should rather use Javascript.

我们应该使用 Javascript。

    <button href="images/car.jpg" id="myButton">
        Here is the Button to be clicked
    </button>


    <script>

        $(document).ready(function(){
            document.getElementById("myButton").click();
        });

    </script>