如何使用Javascript在加载时单击按钮

问题描述 投票:0回答:2

我想加载我的页面(https://fiftyallstars.com/tour.html),并希望TOUR自动开始。现在,访问者必须单击ID为“ starttour”的按钮]

这是到目前为止我添加的内容,但是并不能解决问题:

 $(function () {
          window.onload = $('#starttour').click();
      });
<a class="start-tour" href="#" id="starttour"><span class="two skinny" style="margin:10px 5px; width:290px; font-weight:bold; letter-spacing:4px;"><i class="fa fa-photo animated faa-pulse faa-slow"></i> Take a Detailed Tour</span></a>

感谢您的任何帮助。

javascript click onload
2个回答
0
投票
Please check the below snippet. Hope it helps

var tourLink = document.getElementById('starttour');
window.onload=function(){
  document.getElementById("starttour").click();
};

tourLink.addEventListener('click', function () {
   console.log("clicked on page load")
});
<a class="start-tour" href="#" id="starttour">
  <span class="two skinny" style="margin:10px 5px; width:290px; font-weight:bold; letter-spacing:4px;">
    <i class="fa fa-photo animated faa-pulse faa-slow"></i> Take a Detailed Tour
  </span>
</a>

0
投票

谢谢。我认为问题在于页面加载速度太慢,因此我添加了暂停功能。

© www.soinside.com 2019 - 2024. All rights reserved.