Javascript 调整大小函数

标签 javascript jquery html css jquery-events

我在使用根据屏幕尺寸调整元素大小的 javascript 时遇到了问题。我想将这段具有相同功能的代码翻译成 css 简单类。

$(window).resize(function(){ // On resize
   $('item2').css({'height':(($(window).height()))+'px'});
});

我试过了

.test2 {
    height: 100%;
}

但不起作用 - 如果我删除脚本部分元素不是所需的(全屏高度)。

脚本的问题在于它实现了标记的样式,除非使用 !important...,否则无法通过媒体查询更改标记

最佳答案

如果 item2 是一个 class 那么您需要在选择器中添加 .

$(window).resize(function(){ // On resize
  //$('.item2').css({'height':(($(window).height()))+'px'});
  $('.item2').height($(window).height());
});

关于Javascript 调整大小函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43513639/

相关文章:

javascript - JQuery 一次显示一个菜单项

javascript - 错误无法读取未定义的属性 'updaterCacheDirName'

javascript - HTML 表单和 Javascript 问题

html - CSS 位置 float

html - JqueryUI 下拉菜单垂直对齐

html - 从数据库获取 html 标记并显示在 div 中

javascript - 我可以通过 TSLint 或 tsconfig 阻止 IDE 自动包装属性吗?

javascript - 按照与 Ajax 请求相同的顺序处理 Ajax 响应

javascript - Javascript 搜索函数中的错误处理

jquery - jQuery 中的混合字符串和变量