简体   繁体   English

innerHTML在窗口加载时不会改变

[英]innerHTML not changing on window load

Below is the layout of my page. 以下是我页面的布局。

php function to fetch data from db
css that has got some animations for my divs
<script type="text/javascript">
    window.onload = function()
    {
        var myVar = <?php echo callFunctionFromPHP() ?>;
        alert(myVar);
        document.getElementById("waiting").innerHTML = "Text after load";
    }
</script>
<body>
<div id="waiting">Current text</div>

When the page loads, I intend for 'Current text' to be shown and after having loaded page, I want javascript to call php, which internally will fetch data from db, and once done I want to change the div's text to new text. 页面加载时,我希望显示“当前文本”,加载页面后,我希望javascript调用php,它将在内部从db获取数据,完成后,我想将div的文本更改为新文本。

But this is not working. 但这是行不通的。 Although I know that the javascript function is getting called because if in my JS function I only keep the innerHTML line then the page loads with the 'after' text. 尽管我知道正在调用javascript函数,因为如果在我的JS函数中,我仅保留innerHTML行,则该页面将加载“之后”文本。 Also, I know that my php function is working fine because when I do 'inspect page source' it shows the correct data from database. 另外,我知道我的php函数工作正常,因为当我“检查页面源代码”时,它将显示数据库中的正确数据。

but my alert command doesnt work. 但我的警报命令不起作用。 Can someone help me with what is wrong here? 有人可以帮我解决这里的问题吗?

Note : I understand that this is perhaps not the best coding practice (calling php from js function like this), but I am only prototyping for now. 注意:我知道这可能不是最佳编码实践(像这样从js函数调用php),但是我现在只是原型。

您将需要用双引号将输出换行:

var myVar = "<?php echo callFunctionFromPHP() ?>";

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

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