donglianer5064 2017-08-15 00:28
浏览 59
已采纳

当PHP回应时,Onclick确认()无效

I'm trying to use a javacript confirm() box via the PHP echo below and it's not working.

echo '<a href="http://sdfsdfs.com?keepThis=true&TB_iframe=true&height=10&width=1200&x=2342342&enter_other_room=2" class="box" TARGET="_self" oncontextmenu="return false;" onclick="confirm(\"Are you sure you want to enter the users\'s Room?\");\">User\'s Room</a>';

What am I missing here?

  • 写回答

2条回答 默认 最新

  • drjun1994 2017-08-15 00:36
    关注

    You can't escape quotes inside HTML attributes, so onclick="confirm(\"...\");" won't work. Put single quotes around the string. And since it's inside a PHP single-quoted string, you need to escape them for PHP.

    And since the string contains an apostrophe inside it, you need to escape that for Javascript. You have to double the backslashes to get a literal backslash into the PHP string. And a third backslash to escape it for PHP.

    echo '<a href="http://sdfsdfs.com?keepThis=true&TB_iframe=true&height=10&width=1200&x=2342342&enter_other_room=2" class="box" TARGET="_self" oncontextmenu="return false;" onclick="confirm(\'Are you sure you want to enter the users\\\'s Room?\');\">User\'s Room</a>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改