我试图通过下面的PHP echo使用javascript Confirm()框,但它无法正常工作。

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>';

我在这里想念什么?

最佳答案

您无法在HTML属性中转义引号,因此onclick="confirm(\"...\");"将不起作用。在字符串两边加上单引号。并且由于它在PHP单引号字符串内,因此您需要为PHP对其进行转义。

并且由于字符串中包含撇号,因此您需要对Javascript进行转义。您必须将反斜杠加倍才能在PHP字符串中获得文字反斜杠。第三个反斜杠可以将其转为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>';

关于javascript - 当由PHP回应时,Onclick Confirm()无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45684917/

10-11 10:19
查看更多