简体   繁体   中英

How to autoclick a button (stimulate a mouse click on load)?

I am taking a website design class and my first project is to stimulate a mouse click for an audio player when the page loads.

My website is split in 3 files: HTML, CSS and JS.

The current code is:

  function onPlayerReady(event) { var playButton = document.getElementById("play"); playButton.addEventListener("click", function() { player.playAudio(); }); var pauseButton = document.getElementById("pause"); pauseButton.addEventListener("click", function() { player.pauseAudio(); }); } 
  <div class="buttons"> <svg class="button" id="play"> <use xlink:href="#play"> </svg> <svg class="button" id="pause"> <use xlink:href="#pause"> </svg> </div> 

DOM元素具有单击功能,允许您执行此操作。

document.onload(() => document.getElementById('play').click());

With help Of .click() you can fire manually button click method. So in Your Case, if you want to auto click play button then you have to write


    // this function will execute automatically when page  all contents are ready/loaded 
    $(document).ready(function(){
    //add click event lister
    onPlayerReady(event);
    //auto click button with help of
    playButton.click();
    });
Fore More detail you can find jquery document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM