简体   繁体   English

在运行中使用js或jquery更改元素css样式

[英]Change element css styling with js or jquery on the fly

I want to be able to change my navbar position on the fly adjusting remaining content accordingly. 我希望能够动态更改我的导航栏位置,相应地调整剩余内容。

So, I have a functiona website. 所以,我有一个功能网站。 There is a menu navbar on top by default. 默认情况下,顶部有一个菜单导航栏。 What I did is added an 'edit' button to that menu so that when you click it, you get a list of 4 checkboxes with the ability to choose if you want that menu positioned on top (default), left, right or bottom with the other content moving accordingly (eg menu on the left, content goes slightly right, etc). 我所做的是为该菜单添加一个“编辑”按钮,这样当你点击它时,你会得到一个包含4个复选框的列表,可以选择是否要将该菜单放在顶部(默认),左,右或底部。其他内容相应地移动(例如左边的菜单,内容略微正确等)。 So basically, I have something like this: 所以基本上,我有这样的事情:

// css for menu positioning to the left
.navbar-left {
    width: 25%;
    height: auto;
}

.content-right {
    position: relative;
    left: 200px;
}

// css for menu positioning to the bottom
.navbar-bottom {
    position: relative;
    bottom: 0;
    left: 0;
    height: 200px;
}

.content-up {
    position: relative;
    bottom: 200px;
}

And in JS I do something like this 在JS中,我做了类似的事情

if($("#left"):checked){
    $("#menu-bar").addClass("navbar-left");
    $("#content").addClass("content-right");
} else if ($("#bottom"):checked){
    $("#menu-bar").addClass("navbar-bottom");
    $("#content").addClass("content-up");
}

Now, I have much more styling than this, but it is irrelevant to the issue at hand. 现在,我有比这更多的造型,但它与手头的问题无关。 The problem is when I choose 'left' it styles properly but when I change it to 'bottom' after that it still uses the styles from 'left' positioning and adds the new ones to it. 问题是,当我选择'左'时,它的样式是正确的,但是当我将它更改为'bottom'之后,它仍然使用'left'定位的样式并将新的样式添加到它。

Right now I solved the issue by removing the previous classes with .removeClass() method, like that: 现在我通过使用.removeClass()方法删除以前的类来解决问题,如下所示:

if($("#left"):checked){
    $("#menu-bar").removeClass("navbar-bottom navbar-right").addClass("navbar-left");
    $("#content").removeClass("content-up content-down").addClass("content-right");
} else if ($("#bottom"):checked){
    $("#menu-bar").removeClass("navbar-right").addClass("navbar-bottom");
    $("#content").removeClass("content-down").addClass("content-up");
}

Basically, right now I have about a hundred lines of just adding classes of the chosen positioning while removing all the classes of 3 other choices that I added each time. 基本上,现在我有大约一百行只添加所选定位的类,同时删除我每次添加的其他3个选项的所有类。

So, finally the question: Is there any other way to strip all the classes that were used before (just set everything to initial values like when the page was loaded) instead of deleting all these classes by hand? 所以,最后一个问题:是否有其他方法可以去除之前使用过的所有类(只是将所有内容设置为初始值,例如加载页面时),而不是手动删除所有这些类?

I haven't ever tried resetting the classes to their initial state, but you can certainly clear them all off of a single element in one line of code: 我没有尝试过将类重置为初始状态,但你可以在一行代码中清除它们中的一个元素:

To replace all existing classes with another class, we can use .attr( "class", "newClass" ) instead. 要用另一个类替换所有现有类,我们可以使用.attr(“class”,“newClass”)代替。 source: https://api.jquery.com/removeclass/ 来源: https//api.jquery.com/removeclass/

You could probably combine that with the .toggleClass or another method. 您可以将它与.toggleClass或其他方法结合使用。

If I had to sit down and do it right now, based on my understanding of your question I'd just hide the original elements and add new elements with the classes that you'd like, then to revert delete the new elements and restore the original one. 如果我现在必须坐下来做,基于我对你的问题的理解,我只是隐藏原始元素并添加你想要的类的新元素,然后恢复删除新元素并恢复原来的。

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

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