在jquery中创建和分配CSS类

防暴狂暴队长
//Dynamically getting the windows width
var windowwidth = $(window).width() - 50;

//dynamically assigning the windowwidth value to the class(dynamically)
$(window).load(function () {
    $('.cntnt').css('width', windowwidth + 'px');
})


$("#menu-toggle").click(function(e) {
    e.preventDefault();
    $('.wrapper-content').toggleClass('cntnt');
});
#wrapper {
    padding-left: 0;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#wrapper.toggled {
    padding-left: 250px;
}

#sidebar-wrapper {
    z-index: 1000;
    position: fixed;
    left: 250px;
    width: 0;
    height: 100%;
    margin-left: -250px;
    overflow-y: auto;
    background: #4a4f55;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#wrapper.toggled #sidebar-wrapper {
    width: 250px;
}

#page-content-wrapper {
    width: 100%;
    position: absolute;
    padding: 15px;
}

#wrapper.toggled #page-content-wrapper {
    position: absolute;
    margin-right: -250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >

<div class="wrapper-content">
  <div class="container-fluid">
    <a href="#menu-toggle" class="btn pull-left" id="menu-toggle" >
      toggle menu
    </a>
    <div class="row">
      <div class="col-lg-12">
        <h1>Simple Sidebar</h1>
        <p>This template has a responsive menu toggling system. The menu will appear collapsed on smaller screens, and will appear non-collapsed on larger screens. When toggled using the button below, the menu will appear/disappear. On small screens, the page content will be pushed off canvas.</p>
        <p>Make sure to keep all page content within the <code>#page-content-wrapper</code>.</p>
      </div>
    </div>
  </div>
</div>

嗨,我正在尝试在jQuery中创建一个css类,并将其分配给jQuery中的另一个id。主要议程是,我想在jQuery中切换一个类,该类的宽度为$(window),在单击应将类切换为id的函数时,

这是我实现的代码,没有错误,但也没有输出。请帮帮我。

PrzemysławMelnarowicz
$(window).load(function () {
    $('.cntnt').css('width', + windowwidth + 'px');
})

这不是类分配,您只需找到带有该类的元素并设置它们的样式即可。

如果要动态设置类,则应创建新的样式元素,例如:

$(window).load(function () {
    $('body').prepend('<style>.cntnt { width:' + windowwidth + 'px }</style>')
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在python中创建和比较没有变量分配的类实例

CSS 创建和应用类的最佳实践

在C ++中创建和实现类

如何在render()中的map()中创建和分配变量?

在Tkinter应用程序中创建和分配多个变量

如何使用Soot在APK中创建和添加Java类?

在角度类中创建和调用自定义函数

在服务类中创建和注入BCryptPasswordEncoder时出错

类,方法,实例创建和Main等价于Python中的Java

在Pharo Smalltalk中以编程方式创建和删除类

从 Python 中的主文件创建和访问 Settings 类

从变量创建和使用类

在 CSS 中创建和加入非矩形横幅

在jQuery中哪种创建和附加元素的方法更快?

使用 jQuery 在同一模态中创建和更新表单

使用for循环创建和分配不同的变量

如何为列表中的每个项目创建和分配tkinter变量

在一个类中创建和添加字典,在主程序中访问字典

PHP Laravel创建和实例化类

Databricks Pyhton 创建和使用 Customer 类

React - 类的实例创建和使用

如何在VueJS中创建和扩展抽象类组件?

使用实体框架更新 DBContext 类中的创建和修改日期字段

在同一行中创建和使用一个类

如何在 ES6 类构造函数中创建和填充二维数组

在 RStudio 中为新的栅格 k 均值分类创建和设置聚类值

我在 MyPanel 类中创建和显示面板时遇到问题

如何从应用程序中创建和使用的类库中为 TabControl.ItemsSource 创建集合?

toggleClass jQuery类分配