以下是我用来注入(inject)需要应用于 Div 的样式,它结合了这两种样式:.noindex 和 .ms-wpContentDivSpace
<script>
$(document).ready(function(){
$('.noindex, .ms-wpContentDivSpace')..css({
overflow: auto,
height : '150px'
});
});
</script>
<style>
.autoScrolListViewWP
{
HEIGHT: 150px;
OVERFLOW: auto
}
</style>
<script>
$(document).ready
(
function()
{
$('.noindex, .ms-wpContentDivSpace').addClass('autoScrolListViewWP');
}
);
</script>
好吧,但我需要的是上面的新 CSS autoScrolListViewWP 应该只添加到那些 css 组合仅适用于这两个类的元素 .noindex 和 .ms-wpContentDivSpace
但是上面的脚本会将样式应用于存在这两个类的任何 css 组合。例如。 .ms-WPBody 、.noindex 和 ms-wpContentDivSpace autoScrolListViewWP
我的问题是如何仅识别 CSS 类的特定组合?
最佳答案
一个解决方案是获取所有具有这两个类的元素,然后遍历它们并查看它们是否具有其他类。即:
$('.noindex.ms-wpContentDivSpace').each(function(i, elem) {
var classes = elem.className.split(/\s+/);
if (classes.length === 2) { // they should have only these two classes
$(elem).addClass('autoScrolListViewWP');
// alternatively elem.className += ' autoScrolListViewWP'; (untested)
}
});
关于jquery - CSS:仅将样式应用于两个类的组合而不是其他类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14606712/