您需要在活动函数的参数中转义反斜杠,而不是撇号。
$('#league2').append("<input type=\"button\" id=\"2btn\" value=\"1.2\" class=\"butta\" onmousedown=\"active('exam\\'ple','awayteam')");
要使用 php 转义字符串以将其附加到该代码,您可以使用正则表达式。以下将适用于您的情况。
// Replaces a backslash with four backslashes: The 'append' function will interpret it as two backslashes; the 'active' function, as only one.
$str = preg_replace("/\\\\/", "\\\\\\\\\\\\\\\\", $str);
// Replaces an apostrophe with two backslashes and an apostrophe, then javascript will append only one backslash and an apostrophe, escaping that way an apostrophe.
$str = preg_replace("/'/", "\\\\\\\\'", $str);
// Replaces quotations with a backslash and quotations: the quotations will be escaped inside the append function, so one quotations will be printed out on your HTML
$str = preg_replace("/\"/", "\\\\\"", $str);
如果您不知道什么是正则表达式 (regex),我建议您研究一下如何使用它们。他们会帮助你很多。
编辑:由于某种原因,最后一个程序需要双反斜杠才能工作。更新。