jquery - 切换侧边栏不起作用时通过 JQuery 更改属性的 CSS

标签 jquery css

在我的 Rails 应用程序中,我试图根据边栏是否折叠来更改我的 div 的边距。我正在使用 JQuery 的 .css() 方法,但我似乎无法让它工作。

这是我的 JS 代码片段:

document.addEventListener('turbolinks:load', function(){
  $('.openbtn').on('click', function() {
    // This toggles my sidebar
    $('.collapsed-sidebar').toggleClass('displaynone');

    // Checks if the sidebar is collapsed
    if($('.collapsed-sidebar').is(":visible")){

      // So far I have verified that it triggers this alert
      // when I collapse my sidebar
      alert("collapsed sidebar is visible");

      // Class that I am trying to change the CSS Property
      $('.signedin').css({
        'margin-right': '50px !important'
      });
    }

  });
});

这是我的 HTML 结构:

<body>
    <div class="ui sidebar vertical left menu visible very thin icon">
        <div class="collapsed-sidebar displaynone"> // This is the element that I toggle
             // Sidebar content here
        </div>
    </div>
    <div class="pusher">
        <div class="row content">
            <div class="signedin"> // Element that I am trying to change the property
            </div>
        </div>
    </div>
</body

这是我侧边栏的 CSS:

.displaynone {
  display: none !important;
}

.ui.sidebar.vertical.menu {
  -webkit-transition-duration: 0.1s;
  transition-duration: 0.1s;
}

最佳答案

试试这个:

$('.signedin').css("margin-right","50px !important");

关于jquery - 切换侧边栏不起作用时通过 JQuery 更改属性的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334694/

相关文章:

CSS:过渡在 Firefox 扩展中不起作用

html - 为什么 flex 元素不缩小过去的内容大小?

javascript - 对 API 调用中的标点符号进行编码

javascript - jquery 在视口(viewport)中运行一次 .one()

javascript - 使用多个类更改字体

javascript - 如何获取页面加载前的状态码

css - 图像上带有文字的形状

html - 背景图像 + 固定 + 不透明

html - 前 50% 不起作用,父级高度设置为自动

jQuery DynaTree 不工作