如何使用jQuery在asp.net mvc中替换表行颜色? [英] How do I alternate table row colors in asp.net mvc using jquery?

查看:44
本文介绍了如何使用jQuery在asp.net mvc中替换表行颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个愚蠢的问题,但是我是MVC和jQuery的新手.我想替换表的行颜色,并且我决定要使用jQuery来做到这一点.我知道我可以编写一个扩展方法( http://haacked.com/archive/2008/08/07/aspnetmvc_cycle.aspx )等,但在阅读了SH对

Probably a dumb question but I'm new to MVC and jQuery. I want to alternate the row colors of my tables and I've decided that I'm going to use jQuery to do it. I know that I could write an extension method (http://haacked.com/archive/2008/08/07/aspnetmvc_cycle.aspx), etc but after reading SH's comment on the article at http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx I've picked jQuery as the solution I want to implement.

我想实现在 http://www.packtpub上描述的方法.com/article/jquery-table-manipulation-part2 ,但是我还没有弄清楚放置初始jQuery调用的位置(例如:$(document).ready(function(){...});

I want to implement the method described at http://www.packtpub.com/article/jquery-table-manipulation-part2 but I haven't figured out where to put the initial jQuery call (eg: $(document).ready(function() {...});

就像我说的那样,我是jQuery的新手...

Like I said, I'm new to jQuery...

推荐答案

您可以通过在表的所有偶数行上设置类来实现此目的.

You can accomplish this by setting a class on all the even rows of a table.

<html>
    <head>
        <title>Example Title</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('tr:even').addClass('alt-row-class');
            });
        </script>
    </head>
    <body>...</body>
</html>

然后使用标准CSS将样式应用于该类:

Then apply style to that class using standard css:

.alt-row-class { background-color: green; }

评论者正确地指出,您可能希望使用选择器(tr:even)来获得所需的相对于tbodytheadtfoot元素的结果.

A commenter correctly points out that you might wish to play around with the selector (tr:even) to get the results you want relative to tbody, thead and tfoot elements.

这篇关于如何使用jQuery在asp.net mvc中替换表行颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆