asp.net中GridView和jquery操作

彩虹一:奇偶行变色

jQuery :even 选择器

选择每个相隔的(偶数) <tr> 元素:$("tr:even")

jQuery :odd 选择器

选择每个相隔的(奇数) <tr> 元素:$("tr:odd")

让GridView隔行变色

$(document).ready(function() {
 $("#<%=gdRows.ClientID%> tr").filter(":odd").css("background-color", "grey");       
});
我们当然也可以让奇数行变色
$("#<%=GridView1.ClientID%> tr").filter(":even").css("background-color", "#00ff00");

彩虹使用hover()让行变色

$(document).ready(function() {
  $("#<%=gdRows.ClientID%> tr").hover(function() {
    $(this).css("background-color", "Lightgrey");
   }, function() {
   $(this).css("background-color", "#ffffff");
  });
});

这种是针对没有表头th的,有表头的使用has选择器如下:

$(document).ready(function() {
  $("#<%=gdRows.ClientID%> tr:has(td)").hover(function() {
    $(this).css("background-color", "Lightgrey");
   }, function() {
   $(this).css("background-color", "#ffffff");
  });
});

彩虹使用JQUERY在GridView中删除一行

$(document).ready(function () {
            $("#<%=GridView1.ClientID%> tr:has(td)").click(function () {
                $(this).remove();
            });
        });

如果我们想要一个动画效果,让删除的行慢慢消失

$(document).ready(function () {
            $("#<%=GridView1.ClientID%> tr:has(td)").click(function () {
                $(this).fadeOut(1000, function () {
                    $(this).remove();
                });
            });
        });



  

彩虹让鼠标hover的鼠标变成手型

$(document).ready(function () {
            $("#<%=GridView1.ClientID%>  tr:has(td)").hover(function () {
                $(this).css("cursor", "pointer");
            });
        });

                    `TG`}DK~]~XGXI1_HIX%3WC

彩虹拖拽GridView中的行

<script src="Scripts/jquery.tablednd.0.6.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=GridView1.ClientID%>").tableDnD({
                onDragClass: "myDragClass"
            });
        });
    </script>

                        image

.myDragClass
    {
        font-family: Arial;
        font-size : 18pt;
        color : Red 
        background-color:yellow
    }

彩虹遍历GridView中行

$(document).ready(function() {
  $("#<%=gdRows.ClientID%> tr:has(td)").each(function() {
       alert($(this).html());
  });
});

如果我们想包括表头遍历

$(document).ready(function() {
  $("#<%=gdRows.ClientID%> tr").each(function() {
       alert($(this).html());
  });
});

如果我们仅仅想得到表头

$(document).ready(function() {
  $("#<%=gdRows.ClientID%> th").each(function() {
       alert($(this).html());
  });
});

彩虹遍历GridView中单元格中数据

$(document).ready(function () {
            $("#<%=GridView1.ClientID%> tr:has(td)").each(function () {
                var cell = $(this).find("td:eq(1)");
                alert(cell.html());
            });
        });

将会得到表格中第二行的数据,依次弹出张三,李四,王五。。

彩虹 单击得到GridView单元格的数据

 

$(document).ready(function () {
            $("#<%=GridView1.ClientID%> tr:has(td)").hover(function (e) {
                $(this).css("cursor", "pointer");
            });
            $("#<%=GridView1.ClientID%> tr:has(td)").click(function (e) {
                var selTD = $(e.target).closest("td");
                $("#<%=GridView1.ClientID%> td").removeClass("selected");
                selTD.addClass("selected");
                $("#<%=lblSelected.ClientID%>").html('Selected Cell Value is: <b> ' + selTD.text() + '</b>');
            });
        })

                                                image

彩虹让选中的行变色

$(document).ready(function() 
    {
        $('#<%=gdRows.ClientID %>').find('input:checkbox[id$="chkDelete"]').click(function() {
            var isChecked = $(this).prop("checked");
            var $selectedRow = $(this).parent("td").parent("tr");
            var selectedIndex = $selectedRow[0].rowIndex;
            var sColor = '';
            
            if(selectedIndex%2 == 0)
                sColor = 'PaleGoldenrod';
            else
                sColor = 'LightGoldenrodYellow';
                
            if(isChecked)
                $selectedRow.css({
                    "background-color" : "DarkSlateBlue",
                    "color" : "GhostWhite"
                });
            else
                $selectedRow.css({
                    "background-color" : sColor,
                    "color" : "black"
                });
        });     
    });

.prop()获取匹配的元素的属性值。

         image
Top
收藏
关注
评论
原文地址:https://www.cnblogs.com/automation/p/2837794.html