2017-07-06 57 views
0

呼应多个嵌套引用我试图通过PHPPHP - HTML

echo "<a class='fa fa-ban fa-2x cancelClass' onClick='cancelClass('$id', '$formattedDate', '$time')'></a><p class='text-center'>".$formattedDate." @ $time</p>"; 

呼应HTML下面的一行我没有得到一个错误。但onClick ='cancelClass ...的引号不能正确解析,导致javascript函数无法执行。

它是如何被彩色编码在谷歌浏览器的源视图

应该如何获得彩色编码(函数的例子) enter image description here

+0

PHP使单引号和双引号有所不同。你可以尝试在使用 –

+0

'"'一致的https://stackoverflow.com/questions/25916943/uses-for-the-quot-entity-in-html –

回答

2

将其更改为以下

echo "<a class=\"fa fa-ban fa-2x cancelClass\" onClick=\"cancelClass('$id', '$formattedDate', '$time')\"></a><p class=\"text-center\">".$formattedDate." @ $time</p>"; 

转义属性双引号你可以有一个标准化的HTML

+1

啊谢谢。忘记了反斜杠的救生力量。 –

1

你需要做的是这样的:

echo "<a class='fa fa-ban fa-2x cancelClass' onClick=\"cancelClass('".$id."', '".$formattedDate."', '".$time."')\"></a><p class='text-center'>".$formattedDate." @ $time</p>"; 
1

他们得到正确解析,但是你所指定的那些错误的使用。 Javascript不能区分引用字符串变量和引用来包装“onclick”值,它认为onclick过早结束。

为了区分他们,你必须逃避它们。在括号内使用\"

echo "<a class='fa fa-ban fa-2x cancelClass' onClick='cancelClass(\"$id\", \"$formattedDate\", \"$time\")'></a><p class='text-center'>".$formattedDate." @ $time</p>"; 

应该这样做。

另外,不要使用echo整个字符串,只需输出大部分是直接的:

?> //temporarily stop interpreting the file as PHP, so the next bit will be output directly as raw HTML 
<a class='fa fa-ban fa-2x cancelClass' onClick='cancelClass("<?php echo $id;?>", "<?php echo $formattedDate; ?>", "<?php echo $time;?>")'></a><p class='text-center'>".$formattedDate." @ $time</p> 
<?php //continue with PHP 
0

在HTML中使用双引号。 使用带有ENT_QUOTES的htmlspecialchars来转义具有双引号的值。

echo '<a class="fa fa-ban fa-2x cancelClass" onClick="'.htmlspecialchars("cancelClass('$id', '$formattedDate', '$time')", ENT_QUOTES). 
'"></a><p class="text-center">'.$formattedDate." @ $time</p>";