阻止jquery的显示/隐藏添加内联样式?
我有一个情况,我需要根据特定操作(分类)是否正在进行来切换元素的可见性,如果是的话,它是隐藏的,否则它是可见的。
现在我的CSS看起来像这样:
.isCategorizing .category.all {
display:none;
}
即,这意味着如果它的父级具有类.isCategorizing<,则应该隐藏带有类
.category.all
的div /code>
现在,在 jquery 中,如果我切换 .category.all
元素的可见性,则会将内联样式添加到 HTML,优先于 .css 文件中的 CSS!因此,即使上述情况成立,内联样式的可见性值似乎优先,并且元素不会隐藏。在可见 jquery 中添加 style=display:list-item;
到 div 而我的 CSS 应该将其隐藏...
我可以在纯 jQuery 中处理这个问题,但是有太多的状态和检查需要进行确保它应该以这种特定方式工作,并维护一个布尔标志来“记住”该状态是否存在以及是否恢复到该状态。 CSS 方法更简单、更干净,但内联样式令人恼火......
如何最好地处理这个问题?我尝试创建另一个类 .hide
并使用 jquery 切换它。似乎有效,但由于某种原因搞砸了我的元素的定位。我正在玩它,但似乎找不到解决这个问题的干净方法。有什么建议吗?
更新:
这是相应的CSS:
.category{
position:relative;
padding:1px;
margin:2px 0 5px 0;
clear:both;
font-family:arial, sans-serif;
font-size: 14px;
display:inline-block;
width:inherit;
}
.category.all {
display:none;
width:200px;
text-align: center;
padding:3px;
border:2px solid transparent;
border-radius: 4px;
background-color: #DDDFE3;
}
注意:简单的toggleClass在这种情况下不起作用 - 切换“全部”会导致按钮可见,即使它应该可见不会的。其次,我从 .category
类继承了一些属性,因为这个按钮显示在“类别列表”的末尾,可以这么说,并且希望防止重复。
如果我使用toggleClass并复制它可能会起作用的道具,再想一想...让我尝试一下并发布更新
I have a case where I need to toggle the visibility of an element based on whether a particular operation (categorization) is in progress if so, it's hidden, else it's visible.
Now I have css which looks like this:
.isCategorizing .category.all {
display:none;
}
i.e., it means that that div with class(es) .category.all
should be hidden if it's parent(s) have the class .isCategorizing
Now in jquery if I toggle the visibility of the .category.all
element an inline style is added to the HTML taking precedence over the CSS in the .css file!! So even if the above is true the visibility value from the inline style seems to take precedence and the element is NOT hidden. On being visible jquery add's style=display:list-item;
to the div whereas my CSS should make it hidden...
I could handle this in pure jQuery but there are too many states and checks to make sure that it should work in that particular manner as well as maintain a boolean flag to "remember" if that state existed and whether to revert to that state ever. The CSS approach is simpler and much cleaner, but the inline style is the irritating bit...
...how best to deal with this? I tried creating another class .hide
and toggled that with jquery. Seems to work but for some reason screws up the positioning of my element. I'm playing with it but can't seem to find a clean solution to this problem. Any advice??
Update:
Here is the corresponding CSS:
.category{
position:relative;
padding:1px;
margin:2px 0 5px 0;
clear:both;
font-family:arial, sans-serif;
font-size: 14px;
display:inline-block;
width:inherit;
}
.category.all {
display:none;
width:200px;
text-align: center;
padding:3px;
border:2px solid transparent;
border-radius: 4px;
background-color: #DDDFE3;
}
NOTE: A simple toggleClass doesn't work in this case - toggling "all" would cause the button to be visible even when it shouldn't be. Second, I'm inheriting a few props from the .category
class since this button is displayed at the end of the 'category list' so to speak and wanted to prevent duplication.
If I go with toggleClass and just duplicate the props it may work, on second thought...lemme try this and post an update
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过所有澄清后,似乎:
.all
,您希望将其隐藏(无论父级是否具有.isCategorizing
).all
,你希望它被隐藏当且仅当父级有isCategorizing
如果是这种情况,最简单的就是反转
的含义所有
类和使用类partial
代替它。然后您可以这样做:如果废弃
.all
会在您未在此处显示的其他代码中引入问题,您可以继续使用它并引入.partial
(在这种情况下,我我希望将其命名为.not-all
,以便维护者更清楚地了解这种关系)。您可以通过以下方式执行此操作:toggleClass('.not-all')
添加到已有toggleClass('.all')
的一两个位置,然后display
CSS 样式设置器将由新的not-all
类触发After all the clarifications it seems that:
.all
, you want it to be hidden (irrespective of whether the parent has.isCategorizing
).all
, you want it to be hidden if and only if the parent hasisCategorizing
If this is the case, the simplest is to reverse the meaning of the
all
class and use a classpartial
in its place. You can then do:If scrapping
.all
would introduce problems in other code you do not show here, you can keep using it and also introduce.partial
(in which case I would prefer it be named.not-all
so that the relation is more apparent to maintainers). You can do this by:toggleClass('.not-all')
to the one or two places where you already havetoggleClass('.all')
, anddisplay
CSS style setters inside new selectors which will be triggered by the newnot-all
class