document.write问题

时间:2011-01-20 16:24:15

标签: php javascript html

$var = <<<MY_MARKER
<script type="text/javascript">
  function asso(){
  showSpoiler("assoluto");
  var x = document.getElementById("assoluto");
  x.write('$str');
 }
</script>
MY_MARKER;

echo $var;

代码在php部分,这是从php机器中输入的代码:

<script type="text/javascript">

  function asso(){

        showSpoiler("assoluto");

        var x = document.getElementById("assoluto");

        x.write('<div id="log" style="position:absolute;right:0;left:0;top:0;background:black;height:10px;">
<form action="/myblog/index.php" method="POST">
<fieldset>
<legend>Login:</legend>
<label><input name="username" size="9" maxlength="15" type="text"  value="username"></label>
<label><input name="password" size="9" maxlength="15" type="password"  value=""></label>
<label><input name="submit" type="submit" value="Login"></label>
<label><u><a href="index.php?pag=5">Registrati</a></u></label>
</fieldset>
</form>
</div>
');

    }

</script>

来自控制台的错误是Uncaught SyntaxError:x.write(.....)行上的意外标记ILLEGAL;

2 个答案:

答案 0 :(得分:2)

在javascript中,不允许使用多行字符串。 尝试删除每个“\ n”字符:

$str = str_replace("\n","",$str);

答案 1 :(得分:0)

所以用

$str = str_replace(array("\t","\n","\r"), "", $str);

x.innerHTML ='$ str';