我有以下PHP代码来生成一个表,其中包含嵌入其中一列的可点击图像。我知道该函数有效,因为如果在单击时调用sortleads()
,则正确调用JavaScript函数。我试图绕过?sort=IDD
周围的单引号但我在JavaScript控制台中收到以下错误SyntaxError: Invalid escape in identifier: '\'
。我也试过双反斜杠来逃避反斜杠,但这也给出了类似的错误。我应该如何正确地逃避单引号?我还包括所有相关代码。
相关的PHP摘录:
$display_string .= "<th>ID <img src='img/down.png' alt='down arrow'
onclick= 'sortLeads(\'?sort=IDD\')' height='50px' width='50px'> </th>";
完整的PHP:
$display_string = "<table class='table table-bordered table-hover scroller'>";
$display_string .= "<thead class='thead-inverse'>";
$display_string .= "<tr>";
$display_string .= "<th>ID <img src='img/down.png' alt='down arrow' onclick= 'sortLeads(\'?sort=IDD\')' height='50px' width='50px'>
</th>";
display_string .= "</tr>";
$display_string .= "</thead>";
$display_string .= "<tbody>";
使用Javascript:
<script language="javascript" type = "text/javascript">
function sortLeads(query)
{
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState == 4)
{
var results = document.getElementById('sortLeads');
results.innerHTML = request.responseText;
}
}
request.open("GET", "sortLeads.php"+query, true);
request.send(null);
}
</script>
答案 0 :(得分:2)
PHP中存在语法错误
更改强>
$display_string .= "<th>ID <img src='img/down.png' alt = 'down arrow' onclick= 'sortLeads(\'?sort=IDD\')' height = '50px' width = '50px'>
</th>";display_string .= "</tr>";
以强>
$display_string .= "<th>ID <img src='img/down.png' alt='down arrow' onclick=\"sortLeads('?sort=IDD')\" height='50px' width='50px'></th>";
$display_string .= "</tr>";