简体   繁体   English

Javascript更改包含php include的div的内部html

[英]Javascript change inner html of div that contains php include

I am trying to do a javscript where the div contents changes after a certain amount of time to include the next flash animation but javascript is not accepting my code. 我正在尝试做一个javscript,其中div的内容在一定时间后会更改,以包括下一个Flash动画,但是javascript不接受我的代码。 I've tried to escape <?php include 'flash-2.php'; ?> 我试图逃避<?php include 'flash-2.php'; ?> <?php include 'flash-2.php'; ?> with this <?php include \\'flash-2.php\\'; ?> <?php include 'flash-2.php'; ?>与此<?php include \\'flash-2.php\\'; ?> <?php include \\'flash-2.php\\'; ?>

<script type="text/javascript">
setInterval(function()  
        {
        var res = "<?php include \'flash-2.php\'; ?>";
        document.getElementById("swfdiv").innerHTML = res;
        }, 
        3000);
</script>

PHP code is executed BEFORE the page is rendered to the user. 在将页面呈现给用户之前,将执行PHP代码。 Always. 总是。 No exceptions. 没有例外。 By the time your JS runs, it will do nothing. 到您的JS运行时,它什么也没做。

If you want to include something in your page after it's already loaded, you need to look at an asynchronous technology, like AJAX. 如果要在页面加载后包括某些内容,则需要查看异步技术,例如AJAX。

If you want to show output of you PHP file in a DIV, then call your php file through AJAX and then update the innerHTML of the DIV. 如果要在DIV中显示PHP文件的输出,请通过AJAX调用php文件,然后更新DIV的innerHTML。

For example if you use jQuery then you can do like, 例如,如果您使用jQuery,则可以这样做,

$.get( "flash-2.php", function( data ) {
  $("#swfdiv").html(data);    
});

Hope it helps, thanks. 希望能有所帮助,谢谢。

Ajax is your best bet: Ajax是您最好的选择:

<script type="text/javascript">
setInterval(function()  
{
        $.ajax( "example.php" )
    .done(function(res) {
            document.getElementById("swfdiv").innerHTML = res;
    })

}, 
 3000);
</script>

use ajax like this 这样使用ajax

 <script type="text/javascript">
 setInterval(function()  
 {
    $.ajax({
        type: "POST",
        url: flash-2.php,
        success: function(res){
            if(res)
            {
                document.getElementById("swfdiv").innerHTML = res;
            }
        }
    }), 
    3000);
</script> 

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

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