无法将 PHP 文件加载到使用 javascript 动态创建的内部 HTML Div 中
我只需使用 即可将 .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>
动态创建的第二个 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
I am able to load a .php file into a div simply by using <?php include("file.php") ?>
The file can have php, javascript, html and plain text in it and it still works great.
I am NOT able to load the file into a Div I created dynamically with javascript if the file has script tags in it.
NOTE: I am updating the code as I make progress
index.php
The first Div works great and loads the test.php file with no issues.
<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>
The Second Div created dynamically will not load the file if it has script tags in it
<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 <-the file i'm trying to load
<?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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说它不起作用,因为你在设置innerHTML的单引号语句中使用了单引号。
编辑:
您可以尝试自动删除换行符,如下所示:
将 $file 放入您的innerHTML中
I would say it doesn't work because you use single quotes into your single quotes statement setting your innerHTML.
Edit:
You can try to strip newlines automatically like so:
put $file into your innerHTML
尝试看看这段代码。我成功了。创建您的index.php代码并将Jquery文件放入该目录中。从这里下载它 http://jquery.com/download /。
Try to see this code . I was successful with it . Create your index.php code and put the Jquery file in that directory.Download it from here http://jquery.com/download/.