简体   繁体   English

Javascript / JQuery - 页面加载后动态运行代码

[英]Javascript/JQuery - Running Code Dynamically Some Time After Page Load

I am having trouble with running some javascript code, the code is: 我在运行一些javascript代码时遇到问题,代码是:

<script type="text/javascript">
    function hidesolutions()
    {
        $('.optiondiv').hide();
    }
    hidesolutions()
</script>

The flow is: 流程是:

HTML page loads User clicks a button Javascript function runs & Calls a PHP script that outputs data to the page Javascript function finishes PHP script finishes HTML页面加载用户单击按钮Javascript函数运行并调用将脚本输出到页面的PHP脚本Javascript函数完成PHP脚本完成

The PHP script outputs a div with class "optiondiv" that I want to hide as soon as the PHP is done. PHP脚本输出一个带有“optiondiv”类的div,我想在PHP完成后立即隐藏它。 I cannot hide it in the javascript that runs when the button is clicked as this finishes before the PHP is done. 我无法在点击按钮时运行的javascript中隐藏它,因为这在PHP完成之前完成。

Essentially I'm trying to insert the above script into the HTML at some random point in time, but obviously this means it does not run, so I've tried echoing it out in the PHP - does not work and I've tried putting it in the javascript, but unfortunately the javascript finishes before the PHP does and does not find anything to hide. 本质上我试图在一些随机的时间点将上述脚本插入到HTML中,但显然这意味着它不会运行,所以我尝试在PHP中回显它 - 不起作用我试过放它在javascript中,但遗憾的是javascript在PHP之前完成,并且没有找到任何隐藏的内容。

EDITED to make clearer as per comments. 根据评论编辑以使其更清晰。

I'm assuming you're using ajax to pull in your data from the php file. 我假设您正在使用ajax从php文件中提取数据。 You'll need to call this function in the success callback of your ajax request. 您需要在ajax请求的成功回调中调用此函数。

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

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