javascript - 我的 css 类没有通过,使用 JQuery toggleClass

标签 javascript jquery css toggleclass

好的,所以我基本上有了这个列表。我想做的只是用 toggleClass 突出显示列表中的元素。

<div class="listbox">
    <ul>
        <li>111</li>
        <li>222</li>
        <li>333</li>
        <li>444</li>
        <li>555</li>
    </ul>
</div>


<script type="text/javascript">
    $('#container .listbox li').click(function() {

        $(this).toggleClass('highlight');

    });
</script>

CSS:

div.listbox ul li{
    background: #eee;
    padding: 15px 20px;
    border-bottom: 1px solid #999;
    border-top: 1px solid #fff;}

头:

<style type="text/css">
    .highlight{
        background: #aaa;
        padding: 20px 15px;
        border-bottom: 1px solid #fff;
        border-top: 1px solid #999;}
</style>

问题是,如果我在 .css-doc 中指定了一个 css 属性,我就不能用新类修改那个值。

我试过将它放在 styleattributes 的上方和下方,如下所示,放在头部区域。

点击时,js 添加类和所有,但任何预定义的值都不会改变。

最佳答案

尝试增加等级 specificity对于类似的东西:

div.listbox ul li.highlight {

关于javascript - 我的 css 类没有通过,使用 JQuery toggleClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4659490/

相关文章:

javascript - 当 html 内容包含使用 css 加载的图像时,ng-bind-html 不起作用

Javascript:数组中的对象,包含带有id的对象

javascript - 输入两个数字后自动切换到下一个输入字段

jquery - 有没有办法在 jQuery 选择器中执行 OR 操作

javascript - 如何在初始化后更改 jQuery 对话框 html 内容?

html - 如何使用 ':hover' 更改 CSS

javascript - Bootstrap 未按预期工作

jquery - <head> 中的导航栏按钮随着 <body> 中的网格填充移动(向下扩展)

iphone - 从 iPhone 优化页面中删除水平移动/摆动

html - 对齐 li 菜单项中心