jquery - 如何使用 jquery 添加/删除类或样式

标签 jquery css

我有以下代码,它显示嵌套的 ul 并在单击时隐藏其他打开的。我的问题是如何向打开嵌套 ul 的父 li a 添加背景图像并从它关闭的父 li a 中删除背景图像?

这是我的 jquery:

$(document).ready(function() {
    $('ul ul').hide();

    $('ul li > a').click(function(event) {
        $('ul ul').hide('slow');
        $(this).parent().find('ul').show('slow');
    });

});;

最佳答案

// will add the class
$('#item').addClass('myClass');

// will remove the class
$('#item').removeClass('myClass'); 

// will toggle the class (add it if doesn't have it or remove it if it does)
$('#item').toggleClass('myClass'); 

和内联样式;

// will override those properties
$('#item').css({'color':'red','background':'blue'}); 

关于jquery - 如何使用 jquery 添加/删除类或样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8913086/

相关文章:

javascript - 如果有任何输入获得焦点,则不执行函数

javascript替换换行符和特殊字符

javascript - vue @click 函数 on::after css element - close btn

html - Openlayer弹出位置是底部中心

javascript - 更新 CSS 规则属性值

javascript - 查找元素相对于视口(viewport)或文档的整数像素位置 : getBoundingClientRect?

javascript - 当我将鼠标悬停在按钮的边缘时,如何让我所有的栏都变成黑色?

javascript - 如何创建在 mouseOver 上滚动的内容区域(图片库)

jquery - 为什么这个 jQuery 代码无法在 Firefox 中改变背景颜色?

Javascript 加号运算符不适用于三元 if else