jquery-問題解答

來源:互聯網
上載者:User

標籤:jquery

1、var v = $(‘.summer-input:input‘).val(); //根據class擷取input

2、var v = $(‘input[name=user\\.name]‘).val(); //轉義帶. 的name  轉義 \\

3、var v = $(‘input:eq(-2)‘).val(); //擷取div內倒數第二個input元素  倒數可以寫負數

4、對象[]  $(‘div > span‘).html() 為 one   // div >span 為div下的子項目;div span  為div內所有的

5、if($(‘#content‘).length>0){ $(‘#content‘).remove();}  //判斷一個元素是否存在,$().length>0

6.1、
function selectCheckbox(){
       $(‘#table thead input[type=checkbox]‘).click(function(){  //checkbox 點擊事件
           if($(this).is(":checked")){
               $(‘#table tbody input[type=checkbox]‘).prop(‘checked‘,true);   //checkbox 判斷是否選中, $().prop(‘checked‘)==true?false/$().is(‘:checked‘)/$(‘  :checked‘).length
               $(‘#table tbody tr‘).attr(‘bgcolor‘,‘red‘);
           }else{
               $(‘#table tbody input[type=checkbox]‘).prop(‘checked‘,false);
               $(‘#table tbody tr‘).attr(‘bgcolor‘,‘#FFF‘);
           }
       });
}

6.2/6.3、
function addClass(){
       $(‘#table tbody input[type=checkbox]‘).click(function(){
           if($(this).prop(‘checked‘)){
               $(this).parent("td").parent("tr").attr(‘bgcolor‘,‘red‘);  // 向上找尋 tr :  $().parent(‘td‘).parent(‘tr‘)
           }else{
               $(this).parent("td").parent("tr").attr(‘bgcolor‘,‘white‘);
           }
           if($(‘#table tbody tr‘).length==$(‘#table tbody input[type=checkbox]:checked‘).length){
               $(‘#table thead input[type=checkbox]‘).prop(‘checked‘,true);
           }else{
               $(‘#table thead input[type=checkbox]‘).prop(‘checked‘,false);
           }
       });
}

7.1、
function ajaxqq(){
       $.ajax({
           type:‘post‘,
           url:‘a.html‘,
           data:{
                username:‘王‘  //接參數 {}
           }
       });
}

7.2/7.3、
function jxJsonData(){
       var jsonData = {"userList":[{"id":"1","name":"玩家","age":"18"},{"id":"2","name":"玩家2","age":"18"},{"id":"3","name":"玩家3","age":"18"}],"pageInfo":["aa","bb","cc"]};
       var htmls = "<table>";
       $.each(jsonData.userList,function(index,value){   //迭代
            htmls = htmls + "<tr><td>"+value.id+"</td><td>"+value.name+"</td><td>"+value.age+"</td></tr>";
       });
       htmls = htmls+"</table>";
       $(htmls).appendTo("body");  
       
       var pages = "";
       $.each(jsonData.pageInfo,function(index,value){
           pages = pages+index+":"+value+",";
       });
       $("<p>"+pages+"</p>").appendTo("body");
}

8.1、
function ssUL(){
       $("ul>li>a").next("ul").hide();
       $("ul>li>a").click(function(){
           $(this).next("ul").toggle();   //toggle 函數 隱藏或顯示
       }); 
}

8.2、
function openWindow(){
       $("<iframe id=‘iframename‘ name=‘iframename‘ style=‘width:160;height:160;‘></iframe>").appendTo("body");
       $(‘ul li a‘).on(‘click‘,  function() {
           $(this).attr(‘target‘,‘iframename‘);  //超連結的內容開啟在iframe中 1、給超連結添加target屬性,值為iframe的name 2、給iframe添加src值
           $(‘#iframename‘).attr(‘src‘,$(this).attr(‘href‘));
       });
}

jquery-問題解答

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.