我在 php 文件中转义这个 js 函数时遇到了麻烦: 它返回错误: JavaScript:语法错误:参数列表后缺少 ) 代码是这样的:
echo "<a href=\"\" onClick=\"show_confirmation('",$messages['deleting_message'],"', '",$messages['close'],"', 'show_hide('confirmation');', '",$lang['delete'],"', 'redirect_communicator('",$fullurl,"');'); return false;\">Delete</a>";
基本上这是一个有5个参数的js函数,其中一些也是函数,存在问题。像 $messages[] 这样的所有变量都是语言定义的变量。错误显示最后一个参数 - 函数redirect_communicator()
你能告诉我出了什么问题以及如何避免它吗? 谢谢您的建议
编辑: 我也尝试过这个:
<a href="" onClick="show_confirmation('<?php echo $messages['deleting_message'];?>', '<?php echo $messages['close'];?>', show_hide('confirmation');, '<?php echo $lang['delete'];?>', redirect_communicator('sda');) return false;">delete</a>
仍然是同样的错误..当我添加 json_encode 时,又引发了另一个错误..
最佳答案
像这样混合使用语言总是很难阅读,而且很少是一个好主意。
但是,如果您需要这样做,可以使用 sprintf
来帮助澄清一下:
echo sprintf(
"<a href=\"\" onClick=\"show_confirmation('%s', '%s', show_hide('confirmation'), '%s', redirect_communicator('%s')); return false;\">Delete</a>",
$messages['deleting_message'],
$messages['close'],
$lang['delete'],
$fullurl
);
关于JavaScript:语法错误:参数列表后缺少 ) - 如何在 php 中正确转义 js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34206254/