[英]Can't get right syntax for JavaScript variables inside php print
我为测试按钮内容而生成的以下代码有什么问题?
我得到语法错误“意外T_CONSTANT_ENCAPSED_STRING”
<?php /* Created on: 14-9-2013 */ ?>
<html>
<head>
<script type="text/javascript" charset="utf-8">
var mldkr = Math.round(screen.availWidth/2.08);
var supro = Math.round((screen.availHeight)/6), alto=supro*4
var urlesp = "http://translate.google.com/#<?php echo $fontLingvo ?>|eo|<?php echo $fontVorto ?>";
</script>
</head>
<body>
<table>
<?php
print '
<tr height="30">
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td align="right">
<button type="button" onclick="fenEspo = window.open(urlesp, '', 'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height='+alto+', top='+supro+', left='+mldkr+', width='+mldkr, true)" title="Ne gravas, ne kalkuliĝas por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
</td>
<td align="center">
<input type="radio" name="eo" value="1" />jes
<input type="radio" name="eo" value="0" />ne
<input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
</td>
</tr>
'
?>
</table>
</body>
</html>
谢谢!
如果你想使用'
内部'
,你必须逃避它们。
// "usual" string
$foo = 'abc';
// string with ' as content
$bar = ' abc \' def ';
在您的示例中,查看对window.open()
的调用。
<?php
print '
<tr height="30">
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td align="right">
<button type="button" onclick="fenEspo = window.open(urlesp, \'\', \'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height='+alto+', top='+supro+', left='+mldkr+', width='+mldkr+', true)" title="Ne gravas, ne kalkuliĝas por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
</td>
<td align="center">
<input type="radio" name="eo" value="1" />jes
<input type="radio" name="eo" value="0" />ne
<input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
</td>
</tr>
';
?>
为了更清晰的代码库,您应该将该代码移动到现有<script>
标记中的单独函数中,并在按钮中引用它。 (或在JavaScript中使用addEventListener()
添加事件)
您的报价未正确转义。 有几种方法可以解决这个问题。
定界符:
print <<<EOT
<tr height="30">
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td align="right">
<button type="button" onclick="fenEspo = window.open(urlesp, \'\', \'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height=\'+alto+\', top=\'+supro+\', left=\'+mldkr+\', width=\'+mldkr, true)" title="Ne gravas, ne kalkuliĝas por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
</td>
<td align="center">
<input type="radio" name="eo" value="1" />jes
<input type="radio" name="eo" value="0" />ne
<input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
</td>
</tr>
EOT;
转义:
print '
<tr height="30">
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td align="right">
<button type="button" onclick="fenEspo = window.open(urlesp, \'\', \'toolbar=0, location=0, directories=0, menuBar=0, scrollbars=1, resizable=1, height=\'+alto+\', top=\'+supro+\', left=\'+mldkr+\', width=\'+mldkr, true)" title="Ne gravas, ne kalkuliĝas por la statistiko.">Kaj kion donas Gugl Trenslejt kiel<br />traduko(j)n por Esperanto?</button>
</td>
<td align="center">
<input type="radio" name="eo" value="1" />jes
<input type="radio" name="eo" value="0" />ne
<input type="radio" name="eo" value="-1" disabled="disabled" /><font size="-3">ne aferkoncerna</font>
</td>
</tr>
';
你有一个额外的'
toolbar=0
'toolbar=0
删除它应该没问题
您需要将双引号转义为: \\"
而不是""
未逃脱的"
将过早地终止字符串。
例:
这是不正确的: "k " is a double quote"
这是正确的: "k \\" is a double quote"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.