当我尝试输入
<script type="text/javascript" >
alert("hello");
</script>
在我的 PHP 页面的评论框中,我没有收到警告框。我在文本文件中看到该脚本,而不是在网页上。由于某种原因<script>
没有执行。我在所有浏览器上都启用了事件脚本和 JavaScript。
My PHP code:
<?php //CFPcomments.php
include_once 'CFPheader.php';
if (isset($_POST['content']))
{
$fp = fopen('comments.txt', 'a');
fwrite($fp, $_POST['content'] . "<br />");
fclose($fp);
}
echo nl2br(file_get_contents('comments.txt'));
echo <<<_END
<h3>Post comment</h3>
<form action='CFPcomments.php' method='post'>
<textarea name='content' rows ='3' cols='100'></textarea>
<br/>
<input type='submit' value='Post' />
</form>
_END;
?>
奇怪。我让它工作了,不知道为什么。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
alert("hello");
</script>
</head>
<body>
</body>
</html>
当我输入此内容时,它似乎有效
有人知道为什么吗???很困惑。
最佳答案
你的nl2br()
很可能正在翻译
<script type="text/javascript" >
alert("hello");
</script>
至
<script type="text/javascript" ><br/>
alert("hello");<br/>
</script><br/>
并破坏 JavaScript 代码。
关于javascript - 为什么 <script> 标签在此 PHP 页面上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23099981/