Automatyczne kliknięcie elementu przycisku podczas ładowania strony przy użyciu jQuery

Gdybym chciał automatycznie kliknąć element przycisku podczas ładowania strony, jak bym to zrobił używając jQuery?

Przycisk html to

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

Każda pomoc w tym temacie będzie bardzo mile widziana! Z góry dzięki!

Author: JStormThaKid, 2013-09-06

5 answers

Po prostu użyłbyś jQuery w ten sposób...

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

Użyj funkcji click, aby automatycznie kliknąć przycisk # modal

 32
Author: JStormThaKid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-05 22:22:41

JavaScript Czysty:

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

JQuery:

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

Lub

<script type="text/javascript">
$(document).ready(function(){
    $("#modal").click(); 
});
</script>
 18
Author: KingRider,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-12 14:40:46

Użyj następującego kodu

$("#modal").trigger('click');
 8
Author: user3164444,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-19 10:29:15

Wypróbowałem następujące sposoby w najpierw jQuery, potem JavaScript:

JQuery :

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

To jest najlepszy sposób w JavaScript :

 document.getElementById("id").click();
 5
Author: Ravi Mane,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-30 22:59:17

Powinniśmy raczej używać 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>
 0
Author: mhtmalpani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-25 21:28:45