Home > Web Front-end > JS Tutorial > Several ways to control the display and hiding of TR with jQuery_jquery

Several ways to control the display and hiding of TR with jQuery_jquery

WBOY
Release: 2016-05-16 16:43:56
Original
1540 people have browsed it

There are many online, here are three: The first method is to use id. This method can dynamically set the id of tr when generating HTML. It is also the most commonly used and simplest one, as follows:

  <table><tbody><tr><td>这行不隐藏</td></tr><tr id="tr_1"><td>这行要隐藏</td></tr><tr id="tr_2"><td>这行要隐藏</td></tr></tbody></table> 
Copy after login

Then the control of visibility can be used directly

  for(var i = 1; i < tr_len; i++){ //tr_len是要控制的tr个数  
       $("#tr_"+i).hide();  
  } 
Copy after login

The second method is to use $.each(). This method requires setting the id of the table, as follows:

  <table id="Tbl"><tbody><tr><td>这行不隐藏</td></tr><tr><td>这行要隐藏</td></tr><tr><td>这行要隐藏</td></tr></tbody></table> 
Copy after login

Then the control of visibility can be used directly

  $.each($("#Tbl tr"), function(i){   
     if(i > 0){    
        this.style.display = 'none';  
     }  
  }); 
Copy after login

The third method is through the attribute filter. This method requires adding a specific attribute to tr, such as class, as follows:

  <table id="Tbl"><tbody><tr><td>这行不隐藏</td></tr><tr><td class="hid">这行要隐藏</td></tr><tr><td class="hid">这行要隐藏</td></tr></tbody></table> 
Copy after login

Then the control of visibility can be used directly

  var trs = $("tr[class='hid']");  
  for(i = 0; i < trs.length; i++){   
      trs[i].style.display = "none"; //这里获取的trs[i]是DOM对象而不是jQuery对象,因此不能直接使用hide()方法  
  } 
Copy after login

It’s that simple. If it is to be displayed, change the corresponding method to show() or change the display attribute to "" for practical application: Note: By default, only the row of the "corresponding page name" is displayed. When the radio button is clicked, Show different lines.

  <tr></tr> 
  <td class="tr_title_edit"></td><label for="f_navname">对应页面链接<font color="#ff0000">*</font></label> 
  <td class="tr_content_edit"></td> 
  <input id="f_inner" checked="checked" type="radio" name="f_navState" value="1" /><label for="f_inner">内部链接</label> 
  <input id="f_outer" type="radio" name="f_navState" value="2" /><label for="f_outer">外部链接</label> 
   
   
  <tr id="il" style="display: block"></tr> 
  <td class="tr_title_edit"></td><label for="f_pagename">对应页面名称</label> 
  <td class="tr_content_edit"></td><select id="f_pageid" name="f_pageid"> <option value="" selected="selected"></option> <option value="">新闻</option> <option value="">通知</option></select> 
   
  <tr id="ol" style="display: none"></tr> 
  <td class="tr_title_edit"></td><label for="f_navname">外部链接</label> 
  <td class="tr_content_edit"></td><input id="f_outsidelink" class="inputLine" size="40" name="f_outsidelink" type="text" /> 
   
Copy after login

Control hiding and display by id as follows:

  $("input[name='f_navState']").click(function(){ 
       //if($("input[name='f_navState']").attr("checked")==true){ 
        $("input[name='f_navState']").each(function(i){ 
         if(this.checked){ 
           var f_navState = $("input[name='f_navState']")[i].value;  //获得单选框的值 
           if(f_navState==1){ 
            //alert(123); 
            $("#il").show(); 
            $("#ol").hide(); 
           }else{ 
            //alert(456); 
            $("#ol").show(); 
            $("#il").hide(); 
           } 
             
        } 
        }); 
       //} 
    
   }); 
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template