如何在php变量中绑定html的正确语法

如何在php变量中绑定html的正确语法,php,jquery,Php,Jquery,我在php中有一个变量,其中包含一个包含inclick事件的输入,如下所示: $SelectButton = '<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, "id1")" value="Select All and Copy" /><br>'; $('.surroundpre', this).prepend('$Se

我在php中有一个变量,其中包含一个包含inclick事件的输入,如下所示:

$SelectButton = '<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, "id1")" value="Select All and Copy" /><br>';
$('.surroundpre', this).prepend('$SelectButton');
当我使用浏览器查看源代码时,我看到创建的以下代码:

<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, " id1")"="" value="Select All and Copy">

如您所见,在id1之前是一个空格,并且在值之前有一个“=”
因此,语法肯定有问题,但我无法找出问题所在

您需要在
onclick
属性中使用单引号并将其转义。请尝试以下操作:

$SelectButton = '<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, \\\'id1\\\')" value="Select All and Copy" /><br>';
$SelectButton='
';
您需要在
onclick
属性中使用单引号并将其转义。请尝试以下操作:

$SelectButton = '<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, \\\'id1\\\')" value="Select All and Copy" /><br>';
$SelectButton='
';
我尝试了这一点,但现在按钮不再出现,这似乎是一个单独的问题。您是否检查了PHP日志或JS控制台中的错误?您是对的,有一个语法错误,在参数列表后显示:Uncaught SyntaxError:missing这是解析的内容,在这一行中有错误:$('.surroundpre',this).prepend(“
”);您还需要在输出中的
之前包含\项,请检查我的更新答案。我尝试了此操作,但按钮现在不再出现,这似乎是一个单独的问题。您是否检查了PHP日志或JS控制台中的错误?您是对的,在参数lis之后出现了一个语法错误,上面写着:Uncaught SyntaxError:missing)t这是解析的内容,在这一行中有错误:$('.surroundpre',this).prepend('
');您还需要在输出中的
'
之前包含\,请检查我的更新答案。