我有以下代码,它显示嵌套的 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/