简体   繁体   English

仅在页面加载后在javascript中调用函数

[英]Call a function in javascript only after page load

I have a button that is set up to call a javascript function to automatically activate a "tab" on a page. 我有一个设置为调用javascript函数以自动激活页面上的“标签”的按钮。 When that tab is loaded, there are subtabs that display. 加载该选项卡后,将显示子选项卡。 I am trying to set it up so that one of the subtabs is also automatically activated when the button is clicked. 我正在尝试设置它,以便在单击按钮时也自动激活一个子选项卡。

Currently I can get the first tab to select, but I can't get the subtab to select. 目前,我可以选择第一个选项卡,但是无法选择子选项卡。 #detailsTab is the id of the first tab. #details标签是第一个标签的ID。 #personal-information is the id of the subtab I am trying to select. #personal-information是我尝试选择的子标签的ID。

The following is what I have. 以下是我所拥有的。

This is all in the same file: 这些都在同一个文件中:
HTML: HTML:

<button class="button button--primary button--sm" onclick="viewDetials()">View Details</button>

Javascript: Javascript:

<script type="text/javascript">
    function viewDetials() {
        $("#detailsTab").click();
        personalSubTab();
    }

    window.onload = function personalSubTab() {
        $("#personal-information").click();
    }
</script>

Try combining your functions and adding a short delay for the subtab. 尝试组合您的功能,并为子选项卡添加短暂的延迟。

function viewDetials() {
    $("#detailsTab").click();

    setTimeout(function(){
        $("#personal-information").click();
    }, 200);  // This value can be tweaked

}

The following will be called when the document is ready. 准备好文档后,将调用以下内容。

<script type="text/javascript">
    function viewDetials() {
        $("#detailsTab").click();
        personalSubTab();
    }

    function personalSubTab() {
        $("#personal-information").click();
    }

    // Document ready
    $(function () {
        personalSubTab();
    });
</script>

I'm not exactly sure what you want to do - but if you want to generate a click event on another button/div use trigger, not click - like this: 我不确定您要做什么-但是如果您想在另一个按钮/ div使用触发器上生成click事件,请不要单击-像这样:

function personalSubTab() {
$("#personal-information").trigger('click');}

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

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