1、find()结合each();
$("table tr").find('td').each(function(){
if($(this).html()=="特定值"){
$(this).css("background","yellow");
}
})
jquery each怎么判断index
$(selector).each(function(index,element))
index - 选择器的 index 位置
element - 当前的元素(也可使用 "this" 选择器)
可以将each里某个对象,放入$(this)中,
可以$(this).find() 继续调用find()等方法
$('.sel')//我们选择了三个元素
$('.sel').eq(0)//我们选择了第一个div,它是一个JQ对象,不可以使用dom属性和方法
$('.sel')[0]//我们也是选择了第一个div,但是这是一个dom对象,可以使用dom属性方法,但不可以使用JQ的属性方法
$('.sel').eq(0)[0]//选择第一个div并转换为dom对象,同上
$('.sel')[0].eq(0)///错误,dom对象无法使用JQ方法,因为.eq()是JQ的方法
2、使用变量代替元素选择
$("a[href=' "+变量+" ']");
3、添加css、属性、class、点击按钮、设置文本值、判断元素是否存在、移除on添加的属性、on方法添加属性、查找指定标签内指定属性值的text内容
//css
$('#month1').attr('style',"display:none");
//添加属性
$("#selectchart").attr('onclick','selectchart()');
$("#selectchart").removeAttr('onclick','selectchart()');
//添加class
$("#selectchart").addClass('layui-btn-disabled');
$("#selectchart").removeClass('layui-btn-disabled');
//点击按钮1
var submit = layero.find('iframe').contents().find('#'+ submitID);
submit.trigger('click');
//点击按钮2
$("#CSearch").click();
//设置文本值
$("#p1").text("文本1");
$("#p2").val("文本2");
//判断元素是否存在
if($('.mydiv').length && $('.mydiv').length>0){
console.log("元素存在");
}
if($('#roleId').length && $('#roleId').length>0){
console.log("元素存在");
}
//移除
$(".search").off("click");
//添加click事件
$(".search").on("click",function(){
startTime = $("#monthStart").val();
endTime = $("#monthEnd").val();
reload(1,10);
});
//查找指定标签内指定属性值的text内容
var id=1;
var departmentName=$('option[value=' + id+ ']').text();
获取class值:var classvalue=$("#id").attr("class");
4、添加点击事件移除点击事件
$('#btn').click(function () {}); //添加click事件
$('#btn').on("click",function () {}); //添加click事件
$('#btn').unbind("click"); //移除click事件