将 onClick 操作放入 echo 语句中的语法
以下 onClick 属性的正确语法是什么:
<?php
if($_resp['user']) {
echo '<div class="logo"><a href="http://www.example.com/" onClick="_gaq.push(['_trackEvent', 'Join', 'Home', 'BigButton']);">Link Text</a>';
}
?>
What would be the correct syntax for the onClick attribute in the following:
<?php
if($_resp['user']) {
echo '<div class="logo"><a href="http://www.example.com/" onClick="_gaq.push(['_trackEvent', 'Join', 'Home', 'BigButton']);">Link Text</a>';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您同时嵌套双引号和单引号,因此您需要使用反斜杠对
onClick
中的单引号进行转义。Since you are nesting both double and single quotes, you will need to escape the single quotes in the
onClick
with backslashes.考虑使用 HEREDOC
或退出 PHP 模式:
两者都能完成工作,并消除在生成的 html/javascript 中转义引号的需要
Consider using a HEREDOC
or dropping out of PHP mode:
Both get the job done, and eliminate any need to escape quotes inside the html/javascript you're generating
您可以混合 HTML 和 PHP,因此您可能只想将其移出 PHP:
You can mix HTML and PHP, so you might want to simply move it out of PHP: