首页 新闻 会员 周边

jquery 遍历页面class为gname的td 并且得到其中的值并将其高亮显示

0
悬赏园豆:5 [已解决问题] 解决于 2013-12-18 16:42

html 文本例如:
  <table border="0" cellspacing="0" cellpadding="0" width="100%">
        <tr>
           <td class="gname" width="60%">测试1</td>
    <td class="gname" width="60%">xiao1</td>
    <td class="gname" width="60%">xiao2</td>
        </tr>
    </table>

新麦穗的主页 新麦穗 | 初学一级 | 园豆:82
提问于:2013-12-16 10:21
< >
分享
最佳答案
0
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddClass.aspx.cs" Inherits="CheckBoxDemo.AddClass" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9     <style type="text/css">
10         .test
11         {
12             background-color: green;
13         }
14     </style>
15     <script type="text/javascript" src="jquery-1.10.2.js"></script>
16     <script type="text/javascript">
17         $(function () {
18             $("td.gname").each(function () {
19                 $(this).click(function () {
20                     alert($(this).text());
21                     $(this).addClass("test").siblings("td").removeClass("test");
22                
23                 });
24             });
25 
26         });
27 
28     </script>
29 </head>
30 <body>
31     <form id="form1" runat="server">
32         <div>
33             <table border="1" cellspacing="0" cellpadding="0" width="100%">
34                 <tr>
35                     <td class="gname" width="60%">测试1</td>
36                     <td class="gname" width="60%">xiao1</td>
37                     <td class="gname" width="60%">xiao2</td>
38                 </tr>
39             </table>
40 
41 
42         </div>
43     </form>
44 </body>
45 </html>

测试可以 你可以测试一下

收获园豆:5
wolfy | 老鸟四级 |园豆:2636 | 2013-12-16 13:29
其他回答(5)
0

$("td .gname").addClass("test");

水晶途途 | 园豆:1443 (小虾三级) | 2013-12-16 10:26
0

使用一个jquery循环:

for(var i=0;i<$(".gname").length;i++)

{

     $(".gname").eq(i).css("color","red");//或者其它的高亮标示也行

}

KivenRo | 园豆:1734 (小虾三级) | 2013-12-16 10:29
0

$(function(){ $("td[class='gname']").each(function(){ $(this).css("color","red"); alert($(this).html()); }); });

hjp2009111 | 园豆:202 (菜鸟二级) | 2013-12-16 10:45
0

$('td.gname').each(function(index,item){

    //item为遍历的当前td,是DOM对象

   $(item).addClass('highlight');

   console.log($(item).text());

});

谦行 | 园豆:473 (菜鸟二级) | 2013-12-16 11:13
0

var allvalue="";

$("td.gname").each(function(){

  allvalue=+$(this).text();

  $(this).css("background-color","red");

});

alert(allvalue);

拾梦小侠ด้้้ | 园豆:713 (小虾三级) | 2013-12-16 19:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册