asp.net中GridView和jquery操作(转)
2014-09-24 09:43
585 查看
原文:http://www.cnblogs.com/automation/archive/2012/12/28/2837794.html
一:奇偶行变色
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");
});
});
拖拽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>
.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>'); }); })
让选中的行变色
$(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()获取匹配的元素的属性值。
相关文章推荐
- ASP.NET jQuery 食谱19 (通过jQuery操作GridView技巧集合)
- ASP.NET jQuery 食谱19 (通过jQuery操作GridView技巧集合)(转)
- [翻译] Scott Mitchell 的ASP.NET 2.0数据操作教程之十:使用 GridView 和DetailView实现的主/从报表
- ASP.NET中Gridview读取数据表列表页面常用操作-序号,编辑,删除,删除确认
- ASP.NET 2.0中用Gridview控件操作数据——使用Gridview插入新记录
- 在ASP.NET中使用ObjectDataSource数据控件和GridView操作数据
- 在ASP.NET 2.0中操作数据之十二:在GridView控件中使用TemplateField
- ASP.NET中GridView多行操作
- 使用ASP.NET 2.0 GridView轻松操作数据
- ASP.NET中Gridview读取数据表列表页面常用操作-序号,编辑,删除,删除确认
- ASP.NET中Gridview读取数据表列表页面常用操作-序号,编辑,删除,删除确认
- Scott Mitchell 的ASP.NET 2.0数据操作教程之十:使用 GridView 和DetailView实现的主/从报表
- [翻译] Scott Mitchell 的ASP.NET 2.0数据操作教程之十:使用 GridView 和DetailView实现的主/从报表
- 用jQuery实现asp.net 2.0 treeview控件完全无刷新操作实例(AJAX的增、删、改、选择以及展开收缩节点)
- ASP.NET 2.0中,用GridView控件操作数据
- asp.net操作Gridview单元格
- 使用ASP.NET 2.0 GridView轻松操作数据
- ASP.NET 2.0中用Gridview控件操作数据
- 使用ASP.NET 2.0 GridView轻松操作数据
- 在ASP.NET 2.0中操作数据:在GridView控件中使用TemplateField