媒体查询隐藏/显示表格?

时间:2018-07-18 12:55:21

标签: javascript media-queries

如何使用带有JavaScript的媒体查询在表格(id = firsttable)的隐藏/显示之间切换?我的代码是:

    function myFunction(r) {
        var q = document.getElementById('firsttable');
        if (r.matches) {
                q.style.display = 'none';
            } else {
                q.style.display = 'block';
            }
        }
    var r = window.matchMedia("(max-width: 700px)")
    myFunction(r) 
    r.addListener(myFunction)

1 个答案:

答案 0 :(得分:0)

@executable在评论中回复:

window.onresize = function(event) {
	var w = window.innerWidth;
  if(w > 700){
  	document.getElementById('text').style.display = 'block';
  }else{
   	document.getElementById('text').style.display = 'none';
    }
};
<span id="text">Lorum</span>