简体   繁体   English

javascript意外令牌

[英]javascript Unexpected token <

I'm trying to execute deleteObj function: 我正在尝试执行deleteObj函数:

<td class="tdDesign">
  <a onClick="javascript:deleteObj(<?php echo $row['id'] ?>)">
    <img src="images/deleteSmaller.PNG">
  </a>
</td>

but i'm getting this error: Uncaught SyntaxError: Unexpected token < 但我遇到此错误:Uncaught SyntaxError:意外的令牌<

What is wrong? 怎么了?

edit: $row['id'] is a parameter from database 编辑: $ row ['id']是数据库中的参数

First 第一

Make sure <?php is interpreted, as the < in the error message might actually refer to that. 确保解释了<?php ,因为错误消息中的<可能实际上引用了该内容。 Also make sure you are not already using PHP to output the HTML code you mentioned in your example. 还要确保您还没有使用PHP输出示例中提到的HTML代码。 If this is the case, you do not need another <?php ?> tag, but instead you have to make sure that $row['id'] is treated as variable: 如果是这种情况,则不需要另一个<?php ?>标记,而是必须确保将$row['id']视为变量:

echo '<tag attribute="javascript:do(' . $row['id'] . ');">';

Second 第二

Check whether the contents of $row['id'] express valid javascript. 检查$row['id']的内容是否表示有效的javascript。 Perhaps you want to output a string? 也许您想输出一个字符串? Make sure that your result from $row['id'] is contained in quotes (if it is not a valid expression, such as an integer or a regular expression). 确保$row['id']的结果包含在引号中(如果它不是有效的表达式,例如整数或正则表达式)。

Because PHP is actually running! 因为PHP实际上正在运行! Javascript is seeing <?php echo $row['id'] ?> when you meant it to see the content of row[id]. 当您表示要查看row [id]的内容时,JavaScript看到<?php echo $row['id'] ?>

It might be caused by misconfiguration of the server, or something about the quotation marks that I don't understand. 这可能是由于服务器配置错误或引号引起的,我不明白。

You need to escape the < and > characters in an html/xml attribute using &lt; 您需要使用&lt;转义html / xml属性中的<>字符&lt; and &gt; &gt; entities like following: 实体如下:

<a onClick="javascript:deleteObj(&lt;?php echo $row['id'] ?`&gt;)">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM