gpt4 book ai didi

javascript - 如何通过 javascript onClick 事件方法突出显示一行?

转载 作者:行者123 更新时间:2023-12-03 12:06:15 25 4
gpt4 key购买 nike

我有一个来自数据库的数据列表,我将其显示在表的每一行中。我希望当我单击一行时,它应该保持用颜色突出显示。但我的以下代码突出显示所有行。你能帮我解决一下吗?

它应该仅突出显示我单击的一行。

我的代码

<script type="text/javascript">
function visited(a) {
tr = a.parentNode.parentNode;
tr.style.backgroundColor = "red";
}
</script>

<?php
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

while($res = mysql_fetch_array($get)){
$cdid = $res['cdid'];
$family_name = $res['family_name'];
$given_name = $res['given_name'];
$work_phone = $res['work_phone'];
$mobile_phone = $res['mobile_phone'];
$email = $res['email'];
$email_private = $res['email_private'];
$cid = $res['cid'];
$department = $res['department'];
$title = $res['title'];

$getComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$cid' ");
$resCompany = mysql_fetch_array($getComapnyName);
$companyName = $resCompany['company_name'];

if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). " ... " . substr($companyName, -5);
}else{
$companyName = $companyName;
}
echo "<tr onclick='getDetails($cdid),showNotexBox($cdid),showAllNotesBox($cdid), visited(this);'>";
echo "<td class='' valign='top'>$companyName</td>";
echo "<td class='' valign='top'>$family_name</td>";
echo "<td class='' valign='top'>$given_name</td>";
echo "<td class='' valign='top'>$department</td>";
echo "<td class='' valign='top'>$title</td>";

echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>

最佳答案

如果您想使用 jQuery,您可以执行以下操作。只需复制粘贴代码并运行即可

  <html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>

<script type="text/javascript">


var Color = {
initialize: function() {
$(document).on("click", ".trow", Color.setColor)
},
setColor: function() {

$(".trow").css('background-color','#fff');
this.style.backgroundColor = 'red';
}
};
$(document).ready(function(){
Color.initialize();
});
</script>
</head>
<body>
<table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr class='trow' id='r1'>
<td class='' valign='top'>value1</td>
<td class='' valign='top'>value2</td>
<td class='' valign='top'>value3</td>
<td class='' valign='top'>value4</td>
<td class='' valign='top'>value5</td>
</tr>
<tr class='trow' id='r2'>
<td class='' valign='top'>price1</td>
<td class='' valign='top'>price2</td>
<td class='' valign='top'>price3</td>
<td class='' valign='top'>prive4</td>
<td class='' valign='top'>price5</td>
</tr>
</tbody>
</table>
</body>
</html>

无论如何,您可以使用 jQuery 添加或删除类并使用 css 进行样式设置

关于javascript - 如何通过 javascript onClick 事件方法突出显示一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25182810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com