jquery - 使用 jquery addclass 将 css 添加到表中

标签 jquery css

当有人点击它时,我试图突出显示我的表格中的一行。这是我的表格:

<table class="pretty">
    <tr>
        <td onclick="DoNav('<?php echo $url; ?>');">Name</td>
        <td onclick="DoNav('<?php echo $url; ?>');">Time</td>
        <td onclick="DoNav('<?php echo $url; ?>');">Size</td>
        <td onclick="DoNav('<?php echo $url; ?>');">Length<td>
        <td><input type="checkbox" value="<?php echo $this->result_videos[$i]["video_name"]; ?>" title="Mark this video for deletion" /></td>
    </tr>
...

我已经尝试了一些基于这个 post 的东西:

jQuery("table tr").click(function(){
       var row = jQuery(this);
       var hasClass = row.hasClass("highlight");
       jQuery("table tr").removeClass("highlight");
       if(!hasClass){
         row.addClass("highlight");
         alert("Do I get here?");
       }
});

我的CSS。 编辑:添加了完整的 css,这可能是问题所在:

table.pretty {
width: 100%;
border-collapse: collapse;
font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
clear: both;
}

/* Header cells */
table.pretty thead th {
background: none repeat scroll 0 0 #808184;
border-bottom: 1px solid #2B3D61;
border-top: 4px solid #2B3D61;
color: #ffffff;
font-size: 14px;
font-weight: normal;
padding: 8px;
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
text-align: center;
}

/* Body cells */
table.pretty tbody th {
background: none repeat scroll 0 0 #808184;
border-bottom: 1px solid #2B3D61;
border-top: 4px solid #2B3D61;
color: #ffffff;
font-size: 14px;
font-weight: normal;
padding: 8px;
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
}

table.pretty tbody td {
background: none repeat scroll 0 0 #eeeeee;
border-bottom: 1px solid #2B3D61;
border-top: 1px solid transparent;
color: #333333;
padding: 8px;
text-align: center;
}

table.pretty tbody tr {
cursor: pointer;
}

/* Footer cells */  
table.pretty tfoot th {
background: none repeat scroll 0 0 #808184;
border-bottom: 1px solid #2B3D61;
border-top: 4px solid #2B3D61;
color: #ffffff;
font-size: 14px;
font-weight: normal;
padding: 8px;
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
text-align: left;
}

table.pretty tfoot td {
background: none repeat scroll 0 0 #808184;
border-bottom: 1px solid #2B3D61;
border-top: 4px solid #2B3D61;
color: #ffffff;
font-size: 14px;
font-weight: normal;
padding: 8px;
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
text-align: center;
}


.highlight{
background-color: #a8cb17;
}

出于某种原因,行颜色没有改变。我尝试将 jquery 代码(减去点击)放在 DoNav 函数中,当您点击该行时它会启动视频。

我做错了什么。提前致谢。

最佳答案

您所拥有的应该可以工作,但不必要地复杂。这是同一段代码,但要短得多:

var $rows = jQuery("table tr");
$rows.click(function() {
    $rows.removeClass('highlight');
    jQuery(this).addClass('highlight');
});

这是 fiddle :http://jsfiddle.net/gkxNa/

关于jquery - 使用 jquery addclass 将 css 添加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14405427/

相关文章:

javascript - 如何在 jQuery 中制作可重用的代码?

jquery - jcarousel ie7问题

html - 有没有办法对特定于浏览器的 css3 动画进行分组?

css - 在 CSS 中更改比例点

javascript - 在 jQuery 中搜索和突出显示

javascript - 使用 Jssor slider 对图像进行排序并显示结果

jquery - 为什么 <div> 被拖动后没有响应?

html - 如何防止图像溢出圆 Angular 框?

jquery - 如果 CSS 背景在服务器上发生变化,请使用 jQuery 更新它

html - 当绝对 <div> 嵌套在其中时,相对 <th> 会失去边框