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

jquery常用代码片段整理

2010-12-27 14:36 435 查看
/*字符串操作*/

hdnDis = hdnDis.split(',')[0];//字符串分割

if($("#ImgLock")[0].src.indexOf("success.png", 0) != -1){} //判断src中是否包含'success.png'字符串

j.substring(0, j.indexOf("=")).toLowerCase();//字符串截取并转换为小写

/*select相关*/

var sltdis = $("#ddlDistrict")[0];//获取select对象,也可以写成$("#ddlDistrict").get(0)

sltdis.options.add(new Option(result[i][j].NAME, result[i][j].ID));//给select添加项

sltpro.length = 0;//清空select所有下拉项

var index = sltdis.selectedIndex;//获取select选中项索引

sltdis.disabled = false;//使select不可用(不可操作)

sltdis.val("12");//设置value为12的下拉项为选中状态

/*隐藏控件相关*/

var hdnDis = $("#<%= hidnDistrict.ClientID%>").val();//获取服务器端隐藏控件的值

$("#hidnField").attr("value", "vi_license");//给隐藏控件赋值

$("#hidnField").val("vi_license");//给控件赋值

var sltval = $('input[type=/'hidden/'][id$=/'' + paramArr[0] + '/']').val();//在类型为隐藏控件中找到id为paramArr[0]的控件并获取其值

/*表单相关*/

$("#aspnetForm").submit();//表单提交

/*样式相关*/

$(function() {
$(".GridViewCss").removeAttr("rules");//根据class获取对象并移除rules属性
$(".GridViewCss").css('border-collapse', 'separate');
$(".GridViewCss").attr('border', 0);
//$(".GridViewCss").attr('border-color', '#BFBFBF');
$(".GridViewCss").attr('cellspacing', 1);
$('.GridViewCss').attr('CellPadding', "1px 0px");

$('.GridViewCss tr:gt(0):not(:has(th)) ').each(function() { $(this).bind('mouseover', mover).bind('mouseout', mover) }); //设置鼠标移动的效果

//$('.GridViewCss tr:has(th) th:not(:first-child)').addClass('headto_fh'); //设置标题行的间隔条
//$('.GridViewCss tr:nth-child(1)').addClass('headimage_fh'); //在标题行的下面添加图片
//$('.GridViewCss tr:last-child td').addClass('footColor_fh'); //设置最后以后下面线显示的颜色
$('.GridViewCss tr:not(:last-child):not(:first-child) td').addClass('row_zjf'); //设置除首行和末行外其它行的样式
$('.GridViewCss tr:has(th) th').addClass('head_zjf');
$('.GridViewCss tr:not(has(th)) td').addClass('Allrow_zjf');
$('.GridViewCss').find('tr:nth-child(odd):gt(0)').each(function(index) {
$(this).addClass('AlternatingRowStyle_zjf')
});

$('.GridViewCss tr:not(:first-child)').bind('click', function() {
$(this).find(':radio').attr('checked', true);
});//除首行外各行添加行点击事件
});

function mover() {//添加和移除样式
$(this).find('td').toggleClass('mousemoverclass_zjf');
$(this).find('td:first').toggleClass('mousemoverclass_first_zjf');
$(this).find('td:last').toggleClass('mousemoverclass_last_zjf');
}

/*异步请求*/

$.ajax({
type: "POST",
contentType: "application/json",
url: "../WebSve.asmx/GetTestVehi1",//web服务器方法
data: "{ field:'" + $("#hidnField").val() + "', value: '" + $("#hidnValue").val() + "'}",//给服务器方法参数传值
dataType: 'json',//返回json数据格式
beforeSend: function() {
$('#imgNextVehi').unbind("click");//去除绑定的click事件
$('#imgReTest').unbind("click");
},
success: function(res) {//res为json格式的实体对象
if (res.d.result == "true") {
if (res.d.status == "待测") {
$("#hidnguid").attr("value", res.d.guid);
$("#hidnTerminal").attr("value", res.d.terminal);
$("#hidncom1id").attr("value", res.d.com1id);

$("#imgSerachTest")[0].src = "../App_Themes/Theme1/images/Test/searchTestBlack.png";
$("#imgLookTest")[0].src = "../App_Themes/Theme1/images/Test/lookTestWhite.png";

$("#dvItem").hide();//div隐藏
$("#dvProcess").show();//div显示

Test();
}
else if (res.d.status == "通过") {
alert("此车测试已通过");
return;
}
else {
alert("此车已正式使用");
return;
}
}
else {
alert("此车不存在");
return;
}

},
error: function(result, status) {
if (status == 'error')
alert("测试出错,稍后再试");
}
});
}

/*div相关*/

$('#dvLoading').html("车机测试中...");//改变div的内容

$("#dvItem").hide();//div隐藏

$("#dvProcess").show();//div显示

$("#DvTest").toggle();//可见状态切换

/*事件相关*/

$('#imgReTest').bind("click", function() { Test(); });//绑定事件

$('#imgNextVehi').unbind("click");//去除绑定的事件

/*异步全局错误处理*/
$(function() {
$(document).ajaxError(function(event, request, settings) {
if (request.status == "404") {//请求状态
document.location.href = "../login.aspx";//页面跳转
}
if (request.status == 500) {
var reqst = eval("(" + request.responseText + ")");//把json格式的字符串转化为json对象

$.ajax({
type: "POST",
contentType: "application/json",
url: "../WebSve.asmx/WebServiceMethodExp",
data: "{ userid:'" + $("#hidnUserId").val() + "', errorsource: '" + settings.url + "', message:'" + reqst.StackTrace + "'}",
dataType: 'json',
success: function(result) {
document.location.href = "../MainError.aspx?errid=" + result.d;
},
error: function(result, status) {

}
});
}
});
});

/*未定义对象的判断*/
if (typeof (returnValue) == "undefined") {
return ;

/*集合操作*/

var paramArr1 = $.grep(paramArr, function (val, key) {
return key > 0;
});//取集合的部分项组成一个新的集合,在这里是取除第一项之外的其余项(集合的表示形式如 paramArr="['District','Province','City','Area']")

/*模糊匹配*/

[属性名称] 匹配包含给定属性的元素
[att=value] 精确匹配
[att*=value] 模糊匹配
[att!=value] 不能是这个值
[att$=value] 结尾是这个值
[att^=value] 开头是这个值
[att1][att2][att3]... 匹配多个属性条件中的一个

$("div[id^='T_']")

$("span[id^='spand']")

$("select[name='sltDPCA']")

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