English 中文(简体)
默认的 Javascrap 隐藏列
原标题:Javascript Hide Column by Default

在在线浏览 Javascript 显示/ 隐藏的教程后, 我只能找到所有列都默认可见的例子 。 我正寻找一种方法, 以默认的方式隐藏一些列( 并允许它们通过复选框跳过), 并使用默认显示的列( 并允许它们通过复选框跳过) 。

有可能吗?

供参考,我的表格结构如下:

<table>
  <thead>
<tr>
  <th>Name</th>
  <th>Job</th>
</tr>
  </thead>
  <tbody>
    <tr>
      <td>Mike</td>
      <td>Dancer</td>
    </tr>
  </tbody>
</table>
最佳回答

纯粹的射手:

< 强 > HTML

<input type="checkbox" onclick="showhide(1, this)" checked="checked" /> Name<br />
<input type="checkbox" onclick="showhide(3, this)" checked="checked" /> Job<br />

<强 > JS

function showhide(column, elem){
    if (elem.checked)
        dp = "table-cell";
    else
        dp = "none";
    tds = document.getElementsByTagName( tr );
    for (i=0; i<tds.length; i++)
        tds[i].childNodes[column].style.display = dp;
}

http://jsfiddle.net/NPHDE/3/" rel = “ no follow” >Pure JS小提琴例子

请考虑使用 javascription 库“ a href=”“http://jquery.com/” rel=“nofollow”>JQuery

​ < 强 > HTML

<input type="checkbox" data-col="1" checked="checked" /> Name<br />
<input type="checkbox" data-col="2" checked="checked" /> Job<br />

联合来文:

$(function(){
    $( :checkbox ).on( change , function(){
        $( th, td ,  tr ).filter( :nth-child(  + $(this).data( col ) +  ) ).toggle();
    });
});

http://jsfidd.net/FNFfpA/9/" rel = “ no follow” >jQuery Fiddle example

问题回答

s 在这里切换函数( 使用 jQuery) :

function toggleColumns(column, state) {
    var cells = $("table").find("th, td").filter(":nth-child(" + column + ")");

    if (state)
        cells.hide();
    else
        cells.show();
}

If you need that column hidden by default, you can call this function during onLoad. Example http://jsfiddle.net/nynEd/

有很多办法可以解决这个问题 我的选择是使用基本的拼接函数 比如

<input type="checkbox" id="opt1" checked/>col 1
<input type="checkbox" id="opt2"/>col 2

<table border="1" cellpadding="5">
  <thead>
<tr>
  <th>Name</th>
  <th>Job</th>
  <th id="col1">col 1</th>
  <th id="col2">col 2</th>
</tr>
  </thead>
  <tbody>
    <tr>
      <td>Mike</td>
      <td>Dancer</td>
      <td class="data1">data 1</td>
      <td class="data2">data 2</td>
    </tr>
  </tbody>
</table>​

这是你的 HTML 代码,

$(document).ready(function() {
    if($("#opt1").is(":checked")){
         $("#col1").show();
        $(".data1").show();   
    }else{
         $("#col1").hide();
        $(".data1").hide();
    }
    if($("#opt2").is(":checked")){
         $("#col2").show();
        $(".data2").show();   
    }else{
         $("#col2").hide();
        $(".data2").hide();
    }

    $("#opt1").live( click , function() {
        if($("#opt1").is(":checked")){
         $("#col1").show();
        $(".data1").show();   
    }else{
         $("#col1").hide();
        $(".data1").hide();
    }
    });

    $("#opt2").live( click , function() {
       if($("#opt2").is(":checked")){
         $("#col2").show();
        $(".data2").show();   
    }else{
         $("#col2").hide();
        $(".data2").hide();
    }
    });
});​

这是一个java的密码。

请找到工作 < a href=>" "http://jsfiddle.net/xQnaf/" rel=" nofollow" >example

在你的CS,你应该有一样的东西

.hidden{
    display:none;
}
.shown{
    display:block;
}

然后在您的 html 中,你应该有类似的东西

<table>
  <thead>
     <tr>
        <th id="th1" class="shown">Name</th>
        <th id="th2" class="shown">Job</th>
     </tr>
  </thead>
  <tbody>
    <tr>
      <td id="td1" class="shown">Mike</td>
      <td id="td2" class="shown">Dancer</td>
    </tr>
  </tbody>
</table>

然后,您必须执行一个切换方法,以改变列的可见度

//id should be passhed as 1, 2, 3 so on...
function togleTable(id){
    if(document.getElementById("th"+id).className == "shown"){
        document.getElementById("th"+id).className = "hidden";
    }
    if(document.getElementById("td"+id).className == "shown"){
        document.getElementById("td"+id).className = "hidden";
    }
    if(document.getElementById("th"+id).className == "hidden"){
        document.getElementById("th"+id).className = "shown";
    }
    if(document.getElementById("td"+id).className == "hidden"){
        document.getElementById("td"+id).className = "shown";
    }
}

然后,在 Change () 事件的compabox 中,您应该调用以 id 表示/ 隐藏行数的切换表函数

this is a good place to start i think. Have fun

< 强度 > Uped

if you want to have more than one class for your rows dont forget you can also use this: document.getElementById( id ).classList.add( class ); document.getElementById( id ).classList.remove( class );

您想要隐藏的列应该先有属性样式=“ display: noone” 的属性样式





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签