dqqn32019 2015-12-11 16:19
浏览 55
已采纳

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

I am having a variable in php which contains an input with a inclick event like this:

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

I use this php variable in a javascript to prepend it to a class like this:

$('.surroundpre', this).prepend('$SelectButton');

When I look into the source with my browser, I see this code which is created:

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

As you can see, the is a space before id1 and there is "="" before the value. So there must be something wrong with the syntax but I cannot figure out what is wrong

  • 写回答

1条回答 默认 最新

  • douzhao5656 2015-12-11 16:21
    关注

    You need to use single quotes within the onclick attribute and escape them. Try this:

    $SelectButton = '<input type="button" class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, \\\'id1\\\')" value="Select All and Copy" /><br>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?