简体   繁体   English

Javascript-使用按钮显示和隐藏表格行

[英]Javascript - show and hide table rows with buttons

I'm trying to show/hide table rows with the help of buttons. 我试图在按钮的帮助下显示/隐藏表格行。

With the script I'm using now I'm only I'm only able to make one button and only able to show/hide one row. 我现在使用的脚本只有一个按钮,只能显示/隐藏一行。

I want to make more buttons where each button when pressed will show/hide a row. 我想制作更多按钮,每个按钮按下时将显示/隐藏一行。 Also I want the button text to change between Info▼ and Info▲ as the first one does in my script. 另外,我希望按钮文字像脚本中的第一个一样在Info▼和Info▲之间切换。

If I have understood it correctly I should use classes instead of divs but I tried to do that without result. 如果我对它的理解正确,我应该使用类而不是div,但是我尝试这样做没有结果。

If anyone could help me out with my script so I can produce more buttons where each one will show/hide a unique row when clicked on I would be grateful. 如果有人可以帮我解决我的脚本问题,那么我可以制作更多按钮,单击该按钮时,每个按钮将显示/隐藏一个唯一的行,我将不胜感激。

http://jsbin.com/tovicefu/1/edit This is a simple version of my layout. http://jsbin.com/tovicefu/1/edit这是我的布局的简单版本。 So when I press the first info button the row below should appear with one image. 因此,当我按下第一个信息按钮时,下面的行应显示一张图像。

Same with the other info button but it displays another row further below. 与其他信息按钮相同,但在下面进一步显示另一行。

My script. 我的剧本

<script>

    var button_beg = '<button class="button" onclick="showhide()">', button_end = '</button>'; 
    var show_button = 'Info▼', hide_button = 'Info▲';
    function showhide() {
        var div = document.getElementById( "hide_show" );
        var showhide = document.getElementById( "showhide" );
        if ( div.style.display !== "none" ) {
            div.style.display = "none";
            button = show_button;
            showhide.innerHTML = button_beg + button + button_end;
        } else {
            div.style.display = "block";
            button = hide_button;
            showhide.innerHTML = button_beg + button + button_end;
        }
    }
    function setup_button( status ) {
        if ( status == 'show' ) {
            button = hide_button;
        } else {
            button = show_button;
        }
        var showhide = document.getElementById( "showhide" );
        showhide.innerHTML = button_beg + button + button_end;
    }
    window.onload = function () {
        setup_button( 'hide' );
        showhide(); // if setup_button is set to 'show' comment this line
    }
</script>



<table>
<tr>
<td> <div id="showhide"></div></td> 
<td> </td>
<td> </td>
</tr>


<tr>
 <td>   

    <div id="hide_show">

 <img  src="alfagel.gif" height="210" width="120"/>
    </div>
</td>
<td></td>
<td></td>
  </tr>`


<tr>
<td> <div id="showhide"></div></td> 
<td> </td>
<td> </td>
</tr>

<tr>
 <td>   

    <div id="hide_show">

 <img  src="alfagel.gif" height="210" width="120"/>
    </div>
</td>
<td></td>
<td></td>
  </tr>
</table>

You can use a combination of css and javascript. 您可以结合使用CSS和javascript。 In this jsBin I used a data-attribute to identify 'hideable' rows. 此jsBin中,我使用了数据属性来标识“隐藏”行。

Css: CSS:

tr[data-hide="true"] td {
  display: none;
}

Javascript to toggle visibility: 切换可见性的Javascript:

function toggleRowvisibility(e) {
  var tbl = document.querySelector('table')
     ,rows = tbl.rows;
  for (var i=0;i<rows.length; i+=1){
    var hidable = rows[i].getAttribute('data-hide');
    if (hidable) {
      rows[i].setAttribute('data-hide', /false/i.test(hidable));
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM