[英]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.