Auto-click button element on page load using jQuer

2019-01-13 16:20发布

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!

5条回答
祖国的老花朵
2楼-- · 2019-01-13 16:57

You would simply use jQuery like so...

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

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

查看更多
闹够了就滚
3楼-- · 2019-01-13 16:58

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>
查看更多
做个烂人
4楼-- · 2019-01-13 17:00

Use the following code

$("#modal").trigger('click');
查看更多
forever°为你锁心
5楼-- · 2019-01-13 17:09

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>
查看更多
疯言疯语
6楼-- · 2019-01-13 17:20

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();
查看更多
登录 后发表回答