在动态添加的表行上添加onclick函数

时间:2013-12-28 13:07:11

标签: javascript php html mysql

我有一个显示表中记录的页面,我可以这样做但是在添加代码后突出显示的表行会改变颜色,我现在收到错误。

$result = mysqli_query($con,"SELECT * FROM mydb WHERE `Main Type`='main1' AND `DB Type`='Active' ORDER BY `Record ID`");

echo "<table border='1' style='width:100%; font-family:arial,Serif;font-style:regular;font-size:12px; color:black' CELLPADDING='1' CELLSPACING='0'>
<tr>
<th>Record ID</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>County</th>
<th>Price</th>
<th>Bed</th>
<th>Bath</th>
<th>Square Foot</th>
<th>Year Built</th>
<th>As Is Value</th>
<th>DB Type</th>
<th>Main Type</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr onclick='toggle(this)'>";
  echo "<td>" . $row['Record ID'] . "</td>";
  echo "<td>" . $row['Address'] . "</td>";
  echo "<td>" . $row['City'] . "</td>";
  echo "<td>" . $row['State'] . "</td>";
  echo "<td>" . $row['Zip Code'] . "</td>";
  echo "<td>" . $row['County'] . "</td>";
  echo "<td>" . $row['Price'] . "</td>";
  echo "<td>" . $row['Bed'] . "</td>";
  echo "<td>" . $row['Bath'] . "</td>";
  echo "<td>" . $row['Square Foot'] . "</td>";
  echo "<td>" . $row['Year Built'] . "</td>";
  echo "<td>" . $row['As Is Value'] . "</td>";
  echo "<td>" . $row['DB Type'] . "</td>";
  echo "<td>" . $row['Main Type'] . "</td>";
  echo "</tr>";
  }
echo "</table><br>";

echo "<script type='text/javascript'>";
echo "function toggle(it) { if ((it.style.backgroundColor == 'none') || (it.style.backgroundColor == '')){it.style.backgroundColor = 'yellow';}}";
echo "</script>"


mysqli_close($con);

基本上我的主要目标是,如果用户点击一行,整行将改变颜色以表明它正在被选中。我更进步的目标是使用mousemove而不是onclick。我正在寻找最简单,最简单的方法,我觉得解决方案很简单。我希望你们能帮忙。

我得到的错误是

解析错误:语法错误,意外T_STRING,期待','或';'在第59行的/home/u560877965/public_html/hud.php中

1 个答案:

答案 0 :(得分:3)

您在代码中的字符串之后错过了分号。

改变这个:

echo "</script>"

到此:

echo "</script>";
//              ^ here (for those who can't see it)