Auto-click button element on page load using jQuery

岁酱吖の 提交于 2019-11-26 13:09:40

问题


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

The button html is

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

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


回答1:


You would simply use jQuery like so...

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

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




回答2:


JavaScript Pure:

<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>



回答3:


Use the following code

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



回答4:


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

jQuery:

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

This is the best way in JavaScript:

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



回答5:


We should rather use 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>


来源:https://stackoverflow.com/questions/18646881/auto-click-button-element-on-page-load-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!