如何从字符串中转义引号和特殊字符并将其传递给JS onClick方法?

时间:2015-10-12 06:52:24

标签: javascript php javascript-events onclick

我有PHP脚本/ HTML

$string1 = 'Foo';
$string2 = 'Bar';
$string3 = 'This string contains some URL';
$string4 = 'This string contains multi-line short description which contains single quotes, double quotes and some comma characters as well.';

这是我的onClick

<a class="someClass" title="someTitle" href="#" onclick="someFunction('<?php echo $string1; ?>', '<?php echo $string2; ?>', '<?php echo $string3; ?>', '<?php echo $string4; ?>')">Click Here</a>

但是在接收时,我在someFunction()

上收到了各种字符串解析错误

1 个答案:

答案 0 :(得分:1)

在php方面使用:

<?php echo addslashes($string1); ?>

在someFunction(string1)中使用:

string1 = string1.replace(/\\/g, '');

获取字符串的正常值;