【问题标题】:Unable to load PHP file into a inner HTML Div created dynamically with javascript无法将 PHP 文件加载到使用 javascript 动态创建的内部 HTML Div
【发布时间】:2011-10-13 23:20:47
【问题描述】:

我可以通过使用<?php include("file.php") ?> 将 .php 文件加载到 div 中 该文件可以包含 php、javascript、html 和纯文本,但它仍然可以正常工作。

如果文件中有脚本标签,我无法将文件加载到我用 javascript 动态创建的 Div 中。

注意:我会随着我的进步更新代码

index.php

第一个 Div 效果很好,可以毫无问题地加载 test.php 文件。

<div id="phpDiv"
    style="
    position:absolute; 
    top: 50px; 
    left: 50px;
    height: 200;
    width: 300;
    background-color: #CCCCCC;"> 
        This is the phpDiv  <br>
        <?php include("test.php"); ?>
</div>


如果文件中有脚本标签,则动态创建的 Second Div 将不会加载文件

<script>
    var javascriptDiv = document.createElement('div');
    javascriptDiv.setAttribute('id', 'javascriptDiv');
    javascriptDiv.style.position = 'absolute';  
    javascriptDiv.style.top = 350;
    javascriptDiv.style.left = 50;
    javascriptDiv.style.height = 200;
    javascriptDiv.style.width = 300;
    javascriptDiv.style.background = '#CCCCCC'; //so we can see that the DIV was created

    <?php  
            //must have http:// in it to load php stuff
            //will not load files with <script> tags
            $file = file_get_contents('http://127.0.0.1/Debug/test.php');

            //Trim the string and remove new lines.
            $file = trim($file);
            $file = str_replace("\n", "", $file);
            $file = str_replace("\r", "", $file);

            echo "javascriptDiv.innerHTML = \"This is the javascriptDiv <br>\";  ";  
            echo "javascriptDiv.innerHTML += '". $file ."';";
    ?>

    document.body.appendChild(javascriptDiv); //Display the Window
</script>

test.php

<?php echo "php works <br>"; ?>
<script> alert('javascript works'); </script>
<a> html works </a><br><br>

and extra spaces and plain text work fine as well 

【问题讨论】:

  • test.php 是什么样的?您是否在 PHP 中转义 '?文件中的所有内容都是一行,没有换行符,还是您为 JS 分块和连接该内容?
  • 如果浏览器看到这样的内容:jsfiddle.net/W3k7P 并且它不起作用,那是因为你不能像这样跨两行跨越变量。
  • 您的 DIV 内容需要如下所示:jsfiddle.net/W3k7P/1 注意每一行的字符串连接,每行都被引用。
  • 你渲染的源代码是什么样的? Javascript 控制台说什么?了解这些都非常有用:]
  • 好吧,事情看起来好多了。现在我遇到的唯一问题是动态 Div 加载带有脚本标签的文件。它将加载其他所有内容。

标签: php javascript file dynamic innerhtml


【解决方案1】:

我会说它不起作用,因为您在设置 innerHTML 的单引号语句中使用了单引号。

<?php echo "php works <br>"; ?>
<script> alert("javascript works"); </script>
<a> html works </a><br><br>

编辑:

您可以尝试像这样自动去除换行符:

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('test.php', false, $context);
$file = str_replace( "\r\n", "", $file );

将 $file 放入你的 innerHTML

【讨论】:

  • 如果我从 test.php 中删除除第一行之外的所有内容,它将加载。
  • 另外我不知道是否可以在 innerHTML 语句中使用 javascript :)
  • 您可以在内部HTML 分配中使用 HTML。听起来这是一个多行引用问题。
  • 他应该通过不换行来测试这个,然后我可以告诉他如何去除换行:)
  • 请不要猜测答案是什么。如果您想尝试缩小问题的范围,请使用问题下的 cmets。 :)
【解决方案2】:

尝试查看此代码。我成功了。创建您的 index.php 代码并将 Jquery 文件放在该目录中。从这里下载它http://jquery.com/download/

<html lang="en">
<head>
<meta charset="utf-8">
<title>load demo</title>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="jquery.js"></script>
</head>
<body>
<b>Successful Response (should be blank):</b>
<div id="success"></div>
<b>Error Response:</b>
<div id="error"></div>
<script>
var browser=navigator.userAgent;
//if (browser.lastIndexOf("Chrome")>=5)
{
alert("ok");
$("#success").load("index.php", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
}
</script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多