jQuery 去看官網文檔

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事件

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章