简体   繁体   English

完成 html 文件加载后按下按钮

[英]Press button on completion of loading of html file

I'm quite new to javascript but I encountered the following problem:我对 javascript 很陌生,但遇到了以下问题:

I'm using datatables , which makes javascript tables out of normal html tables.我正在使用datatables ,这使得 javascript 表脱离了正常的 html 表。 That works great.这很好用。 Then I've split the different tables into tabs, as described here .然后我将不同的表拆分为选项卡,如此所述。 This still works.这仍然有效。 But the problem is that I now need to press one of the tab buttons for them to become effective, otherwise all the tables that should be split into the different tabs, appear on top of each other.但问题是我现在需要按其中一个选项卡按钮才能使它们生效,否则应该拆分为不同选项卡的所有表格都出现在彼此的顶部。

Question: How can I make a function that presses a button, when loading of the single page html is complete?问题:当加载单页 html 完成时,如何制作按下按钮的 function? This would resolve my problem.这将解决我的问题。 Or maybe I need to ask the question differently?或者也许我需要以不同的方式提出问题? Why does in the javascript that is linked above that creates the tabs, initially select the first tab?为什么在上面链接的创建选项卡的 javascript 中,最初 select 是第一个选项卡? The code again below.下面又是代码。 What could be the reason that when I use it in conjunction with datatables it no longer automatically selects the first tab?当我将它与数据表结合使用时,它不再自动选择第一个选项卡可能是什么原因?

function openCity(evt, cityName) {
  // Declare all variables
  var i, tabcontent, tablinks;

  // Get all elements with class="tabcontent" and hide them
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }

  // Get all elements with class="tablinks" and remove the class "active"
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }

  // Show the current tab, and add an "active" class to the link that opened the tab
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}

There appear to be a couple of items missing from the "how to" tutorial page which are present in the "try it" page. “尝试”页面中的“操作方法”教程页面似乎缺少一些项目。

1) HTML button ID: 1) HTML 按钮 ID:

Change this...改变这个...

<button class="tablinks" onclick="openCity(event, 'London')">London</button>

...to this: ...对此:

<button id="defaultOpen" class="tablinks" onclick="openCity(event, 'London')">London</button>

2) A $(document).ready(function() {...} action to select the button with this new ID: 2) $(document).ready(function() {...}对 select 具有此新 ID 的按钮的操作:

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();

In my case, I put this line in a <script>...</script> tag at the bottom of the HTML body.就我而言,我将此行放在 HTML 主体底部的<script>...</script>标记中。

Until I made these changes, I had the same issue that you report.在我进行这些更改之前,我遇到了与您报告的相同的问题。

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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