您的位置:首页 > Web前端 > JQuery

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()获取匹配的元素的属性值。

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: